代码之家  ›  专栏  ›  技术社区  ›  linezokode

(UNITY 2D)如何基于父对象的旋转翻转精灵?

  •  0
  • linezokode  · 技术社区  · 3 年前

    我设置了一个东西,使枪沿着围绕玩家的光标方向旋转,我想这样做,当它超过或低于某个旋转时。z值,它翻转枪的精灵。这是我的代码:

     using UnityEngine;
    

    公共课枪击案:单一行为 {

     private Camera _mainCam;
     private Vector3 _mousePos;
     public SpriteRenderer gun;
     public GameObject thisObj;
    
     private void Start()
     {
         _mainCam = GameObject.Find("Main Camera").GetComponent<Camera>();
     }
    
     private void Update()
     {
         RotatePoint();
         RotateGun();
     }
    
     void RotateGun()
     {
         if (thisObj.transform.localEulerAngles.z is < 270f or > 90f)
         {
             gun.flipY = true;
         }
         else if(thisObj.transform.localEulerAngles.z is > 270f or < 90f)
         {
             gun.flipY = false;
         }
         Debug.Log(thisObj.transform.localEulerAngles.z);
     }
     void RotatePoint()
     {
         _mousePos = _mainCam.ScreenToWorldPoint(Input.mousePosition);
         Vector3 rotation = _mousePos - transform.position;
         float rotZ = Mathf.Atan2(rotation.y, rotation.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0,0,rotZ);
     }
    

    }

    这是我的等级制度: enter image description here

    感谢您的帮助,我不知道为什么它不起作用。。。我试过这个。使改变旋转z也是,没有任何成功。

    0 回复  |  直到 3 年前