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;
         }
     }




 }
相关推荐
HH‘HH10 小时前
Unity通过OPC UA工业协议连接工业设备实战指南
unity·游戏引擎
め.19 小时前
定点数运算库
算法·unity
Duo1J1 天前
【UE】Slate 编辑器工具开发03 - 节点编辑器 (EdGraph)
ue5·编辑器·游戏引擎·ue4
郝学胜-神的一滴1 天前
[简化版 GAMES 104] 现代游戏引擎 02:拆解现代游戏引擎5+1层级架构,吃透引擎底层核心逻辑
c++·unity·架构·游戏引擎·图形渲染·unreal engine·系统设计
unityのkiven1 天前
Unity 中如何创建一片森林:享元模式能不能用?应该怎么用?
unity·游戏引擎·享元模式
LONGZETECH1 天前
新能源汽车充电设备装配与调试仿真教学软件 技术架构与核心实现解析
大数据·算法·3d·unity·架构·汽车
丁小未1 天前
Unity 几种常见合批手段的要求
游戏·unity·合批·srpbatcher·动态合批·静态合批
旧物有情2 天前
游戏开发常用架构 #MVP,MVC
游戏·unity·架构·mvc
勇踏前人未索之境2 天前
Unity打包运行于鸿蒙手机
unity·智能手机·harmonyos