Unity3D 镜头跟随鼠标旋转移动

  1. 这里是源码

  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class sheji : MonoBehaviour
  6. {
  7. public int speed = 5;
  8.  
  9. public Transform newobject;
  10.  
  11. float axisX2 = 0;
  12. float axisY2 = 0;
  13. // Use this for initialization
  14. void Start ()
  15. {
  16.  
  17. }
  18.  
  19. // Update is called once per frame
  20. void Update ()
  21. {
  22. float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
  23. float y = Input.GetAxis("Vertical") * Time.deltaTime * speed;
  24.  
  25. transform.Translate(x,0,y);
  26.  
  27. //transform.position = Vector3(0, 0, 0);
  28. //print (x);
  29.  
  30. if(Input.GetButtonDown("Fire1"))
  31. {
  32. Transform n = Instantiate(newobject,transform.position,transform.rotation) as Transform;
  33. Vector3 fwd = transform.TransformDirection(Vector3.forward);
  34. n.rigidbody.AddForce(fwd*2800);
  35. }
  36.  
  37. float axisX = Input.GetAxis("Mouse X");
  38. float axisY = Input.GetAxis("Mouse Y");
  39.  
  40. axisX2 = axisX2 + axisX;
  41. axisY2 = axisY2 + axisY;
  42.  
  43. //按镜头调整方向
  44. var rotation = Quaternion.Euler(-axisY2*speed, axisX2*speed, 0);
  45.  
  46. transform.rotation = rotation;
  47. }
  48. }

你可能感兴趣的:(Unity3D 镜头跟随鼠标旋转移动)