unity 物体转向鼠标点击方向2d和3d

3d:摄像机俯视

2d:Canvas 的Render Mode 修改为 Screen Space -Camera

cs 复制代码
 /// <summary>
 /// palyer
 /// </summary>
 public GameObject palyer;
 /// <summary>
 /// 枪
 /// </summary>
 public GameObject gun;
 /// <summary>
 /// 子弹
 /// </summary>
 public GameObject bullet;
 /// <summary>
 /// 战场位置
 /// </summary>
 public GameObject pos;

 public RectTransform uGUICanvas;

 // Start is called before the first frame update
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {
     //向鼠标点击位置旋转
     if (Input.GetMouseButton(0) && GameControl.instance.isGameStop == false)
     {
         //没有点击到ui上,3d物体旋转
         //if (EventSystem.current.IsPointerOverGameObject() == false)
         //{
         //    //获取鼠标的坐标,鼠标是屏幕坐标,Z轴为0,这里不做转换
         //    Vector3 mouse = Input.mousePosition;
         //    //获取物体坐标,物体坐标是世界坐标,将其转换成屏幕坐标,和鼠标一直
         //    Vector3 obj = Camera.main.WorldToScreenPoint(transform.position);
         //    //屏幕坐标向量相减,得到指向鼠标点的目标向量,即黄色线段
         //    Vector3 direction = mouse - obj;
         //    //将Z轴置0,保持在2D平面内
         //    direction.z = 0f;
         //    //将目标向量长度变成1,即单位向量,这里的目的是只使用向量的方向,不需要长度,所以变成1
         //    direction = direction.normalized;
         //    //当目标向量的Y轴大于等于0.4F时候,这里是用于限制角度,可以自己条件
         //    // 计算旋转角度
         //    float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
         //    // 使用Quaternion.Euler创建旋转
         //    palyer.transform.rotation = Quaternion.Euler(0, angle, 0);
         //    //gun.transform.rotation = Quaternion.Euler(0, angle, 0);

         //}

         //向鼠标点击位置旋转-2d
         if (Input.GetMouseButton(0) && GameControl.instance.isGameStop == false)
         {
             //没有点击到ui上,2d物体旋转
             if (EventSystem.current.IsPointerOverGameObject() == false)
             {
                 Vector3 mousePos;
                 //Canvas必须是摄像机模式
                 RectTransformUtility.ScreenPointToWorldPointInRectangle(uGUICanvas, new Vector2(Input.mousePosition.x, Input.mousePosition.y), Camera.main, out mousePos);
                 float z;

                 if (mousePos.x > palyer.transform.position.x)
                 {
                     z = -Vector3.Angle(Vector3.up, mousePos - transform.position);
                 }
                 else
                 {
                     z = Vector3.Angle(Vector3.up, mousePos - transform.position);
                 }
                 palyer.transform.localRotation = Quaternion.Euler(0, 0, z);

             }
         }

     }
     //鼠标点击发射子弹
     if (Input.GetMouseButtonDown(0) && GameControl.instance.isGameStop == false && GameControl.instance.isPower == true)
     {
         //没有点击到ui上
         if (EventSystem.current.IsPointerOverGameObject() == false)
         {
             GameObject go = Instantiate(bullet);
             go.name = "子弹";
             go.transform.GetComponent<RectTransform>().SetParent(pos.transform.GetComponent<RectTransform>(), false);
             go.transform.GetComponent<RectTransform>().position = gun.transform.position;
             go.transform.rotation = Quaternion.identity;
         }
     }




 }
相关推荐
集芯微电科技有限公司17 天前
四通道2A输出集成功率电感降压模块专为紧凑型方案设计
人工智能·单片机·嵌入式硬件·生成对抗网络·计算机外设
叶帆17 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君17 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
lichong95117 天前
让AI自己用电脑!Cua:后台操作鼠标键盘,Mac/Windows/Linux全支持
人工智能·macos·ai·计算机外设·agent·提示词
会思考的猴子17 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
hai31524754317 天前
九章编程法 · 猜数字游戏 (GW-BASIC 重构版) *
人工智能·microsoft·游戏引擎·游戏程序
心前阳光18 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光18 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
Saniffer_SH18 天前
【高清视频】Gen6 服务器还没到,Gen6 SSD 怎么测?Emily 现场演示三种测试环境
人工智能·驱动开发·测试工具·缓存·fpga开发·计算机外设·压力测试
纪纯18 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico