UNITY3D 脑袋顶血顶名



杨航最近在学Unity3D
using UnityEngine;
using System.Collections;

public class NPC : MonoBehaviour {

        //主摄像机对象
        public  Camera camera;
        //NPC名称
        private string name = "我是doudou";

        //主角对象
        public GameObject hero;
        //NPC模型高度
        float npcHeight;
        //红色血条贴图
        public Texture2D blood_red;
        //黑色血条贴图
        public Texture2D blood_black;
    //默认NPC血值
        private int HP = 100;

        void Start ()
        {
                根据Tag得到主角对象
                //hero = GameObject.FindGameObjectWithTag("Human");
                得到摄像机对象
                //camera = Camera.main;
            blood_red = new Texture2D(100, 10,TextureFormat.RGBA32,false);

            blood_red.SetPixel(100,10, Color.red);
            blood_black = new Texture2D(100, 10, TextureFormat.RGBA32, false);
            blood_black.SetPixel(100,10, Color.black);


 
                //注解1
                //得到模型原始高度
                float size_y = collider.bounds.size.y;
                //得到模型缩放比例
                float scal_y = transform.localScale.y;
                //它们的乘积就是高度
                npcHeight = (size_y *scal_y) ;

        }

        void Update ()
        {
                //保持NPC一直面朝主角
            //    transform.LookAt(hero.transform);
        }

        void OnGUI()
        {
                //得到NPC头顶在3D世界中的坐标
                //默认NPC坐标点在脚底下,所以这里加上npcHeight它模型的高度即可
                Vector3 worldPosition = new Vector3 (transform.position.x , transform.position.y + npcHeight,transform.position.z);
                //根据NPC头顶的3D坐标换算成它在2D屏幕中的坐标
                Vector2 position = camera.WorldToScreenPoint (worldPosition);
                //得到真实NPC头顶的2D坐标
                position = new Vector2 (position.x, Screen.height - position.y);
                //注解2
                //计算出血条的宽高
                Vector2 bloodSize = GUI.skin.label.CalcSize(new GUIContent(blood_red));

                //通过血值计算红色血条显示区域
                int blood_width = blood_red.width * HP / 100;
                //先绘制黑色血条
                GUI.DrawTexture(new Rect(position.x - (bloodSize.x / 2), position.y - bloodSize.y, bloodSize.x, bloodSize.y), blood_black);
                //在绘制红色血条
                GUI.DrawTexture(new Rect(position.x - (bloodSize.x / 2), position.y - bloodSize.y, blood_width, bloodSize.y), blood_red);

                //注解3
                //计算NPC名称的宽高
                Vector2 nameSize = GUI.skin.label.CalcSize(new GUIContent(name));
                //设置显示颜色为黄色
                GUI.color = Color.yellow;
                //绘制NPC名称
                GUI.Label(new Rect(position.x - (nameSize.x / 2), position.y - nameSize.y - bloodSize.y, nameSize.x, nameSize.y), name);


        }

        //下面是经典鼠标点击对象的事件,大家看一下就应该知道是什么意思啦。
        void OnMouseDrag ()
        {
                Debug.Log("鼠标拖动该模型区域时");
        }

        void OnMouseDown()
        {
                Debug.Log("鼠标按下时");

                if(HP >0)
                {
                        HP -=5 ;
                }

        }
        void OnMouseUp()
        {
                Debug.Log("鼠标抬起时");
        }

        void OnMouseEnter()
        {
                Debug.Log("鼠标进入该对象区域时");
        }
        void OnMouseExit()
        {
                Debug.Log("鼠标离开该模型区域时");
        }
        void OnMouseOver()
        {
                Debug.Log("鼠标停留在该对象区域时");
        }

}



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388240.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一个项目的整个测试流程

最近一直在进行接口自动化的测试工作,同时对于一个项目的整个测试流程进行了梳理,希望能对你有用~~~ 需求分析: 整体流程图: 需求提取 -> 需求分析 -> 需求评审 -> 更新后的测试需求跟踪xmind 分析流程: 1. 需…

python度量学习_Python的差异度量

python度量学习Hi folks, welcome back to my new edition of the blog, thank you so much for your love and support, I hope you all are doing well. In today’s learning, we will try to understand about variance and the measures involved in it. Although the blo…

多个摄像机之间的切换

杨航最近在学Unity3D Unity3D入门 第捌章: 多个摄像机之间的切换 内容描述:这章,我们来学习一下同个场景中多个摄像机怎么切换。 接着我们创建一个空对象 GameObject -> Create Empty 命名为CamearController&#xff0…

Kubernetes的共享GPU集群调度

问题背景 全球主要的容器集群服务厂商的Kubernetes服务都提供了Nvidia GPU容器调度能力,但是通常都是将一个GPU卡分配给一个容器。这可以实现比较好的隔离性,确保使用GPU的应用不会被其他应用影响;对于深度学习模型训练的场景非常适合&#x…

django-celery定时任务以及异步任务and服务器部署并且运行全部过程

Celery 应用Celery之前,我想大家都已经了解了,什么是Celery,Celery可以做什么,等等一些关于Celery的问题,在这里我就不一一解释了。 应用之前,要确保环境中添加了Celery包。 pip install celery pip instal…

网页视频15分钟自动暂停_在15分钟内学习网页爬取

网页视频15分钟自动暂停什么是网页抓取? (What is Web Scraping?) Web scraping, also known as web data extraction, is the process of retrieving or “scraping” data from a website. This information is collected and then exported into a format that …

Unity3D面试ABC

Unity3D面试ABC 杨航最近在学Unity3D 最先执行的方法是: 1、(激活时的初始化代码)Awake,2、Start、3、Update【FixUpdate、LateUpdate】、4、(渲染模块)OnGUI、5、再向后&#xff…

前嗅ForeSpider教程:创建模板

今天,小编为大家带来的教程是:如何在前嗅ForeSpider中创建模板。主要内容有:模板的概念,模板的配置方式,模板的高级选项,具体内容如下: 一,模板的概念 模板列表的层级相当于网页跳转…

2.PHP利用PDO连接方式连接mysql数据库

代码如下 <?php$serverName "这里填IP地址";$dbName "这里填数据库名";$userName "这里填用户名&#xff08;默认为root&#xff09;";$password "";/*密码默认不用填*/try { $conn new PDO("mysql:host$serverName;…

django 性能优化_优化Django管理员

django 性能优化Managing data from the Django administration interface should be fast and easy, especially when we have a lot of data to manage.从Django管理界面管理数据应该快速简便&#xff0c;尤其是当我们要管理大量数据时。 To improve that process and to ma…

3D场景中选取场景中的物体。

杨航最近在学Unity3D&#xfeff;&#xfeff;&#xfeff;&#xfeff;在一些经典的游戏中&#xff0c;需要玩家在一个3D场景中选取场景中的物体。例如《仙剑奇侠传》&#xff0c;选择要攻击的敌人时、为我方角色增加血量、为我方角色添加状态&#xff0c;通常我们使用鼠标来选…

xpath之string(.)方法

from lxml import etreehtml <li class"tag_1">需要的内容1<a>需要的内容2</a></li> selector etree.HTML(html ) contents selector.xpath ( //li[class "tag_1"]) contents1 selector.xpath ( //li[class "tag…

循环语句

循环语句&#xff1a; 当我们要做一些重复的操作时&#xff0c;首先想到的是有没有一种循环的语句&#xff1f; 答案当然有 Java提供了三种循环&#xff1a; for循环&#xff0c;在Java5中引入了一种主要用于数组的增强型for循环。while循环do……while循环for循环语法1&#x…

canva怎么使用_使用Canva进行数据可视化项目的4个主要好处

canva怎么使用(Notes: All opinions are my own. I am not affiliated with Canva in any way)(注意&#xff1a;所有观点均为我自己。我与Canva毫无关系) Canva is a very popular design platform that I thought I would never use to create the deliverable for a Data V…

如何利用Shader来渲染游戏中的3D角色

杨航最近在学Unity3D&#xfeff;&#xfeff; 本文主要介绍一下如何利用Shader来渲染游戏中的3D角色&#xff0c;以及如何利用Unity提供的Surface Shader来书写自定义Shader。 一、从Shader开始 1、通过Assets->Create->Shader来创建一个默认的Shader&#xff0c;并取名…

深入bind

今天来聊聊bind 关于之前的call跟apply 查看此链接 我们要明确4点内容 1. bind之后返回一个函数 let obj {name : skr } function fn(){console.log(this) } let bindfn fn.bind(obj) console.log(typeof bindfn) // function 2.bind改变this 并且可以传参 bind之后的函数仍…

Css单位

尺寸 颜色 转载于:https://www.cnblogs.com/jsunny/p/9866679.html

ai驱动数据安全治理_JupyterLab中的AI驱动的代码完成

ai驱动数据安全治理As a data scientist, you almost surely use a form of Jupyter Notebooks. Hopefully, you have moved over to the goodness of JupyterLab with its integrated sidebar, tabs, and more. When it first launched in 2018, JupyterLab was great but fel…

【Android】Retrofit 2.0 的使用

一、概述 Retrofit是Square公司开发的一个类型安全的Java和Android 的REST客户端库。来自官网的介绍&#xff1a; A type-safe HTTP client for Android and JavaRest API是一种软件设计风格&#xff0c;服务器作为资源存放地。客户端去请求GET,PUT, POST,DELETE资源。并且是无…

一个透明的shader

杨航最近在学Unity3D&#xfeff;&#xfeff;Shader "Custom/xiankuang" { Properties { _LineColor ("Line Color", Color) (1,1,1,1) _GridColor ("Grid Color", Color) (1,1,1,0) _LineWidth ("Line Width", float) 0…