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. }
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值