Function to rotate player with mouse position is based on mouse distance instead of position

后端 未结 1 1141
春和景丽
春和景丽 2021-01-28 07:46

I followed an answer on the Unity Forums on how to rotate an object according to the position of the mouse. The code works for changing the rotation, but it uses some other para

相关标签:
1条回答
  • 2021-01-28 07:56

    First things, you seem to imply that there you have this code on two different objects. You should create a single script called "LookAtMouse". Whatever you put this on if what will look at the mouse.

    public Camera cam;
    void Update() {
    
      Vector3 direction = Input.mousePosition - cam.WorldToScreenPoint(transform.position);
      float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
      transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    
    }
    

    So this should be only on the player.

    0 讨论(0)
提交回复
热议问题