如何让摄像机跟随你的角色

111 篇文章 3 订阅
  1. //用来获取一个角色对象
  2. public GameObject target;
复制代码

原先MouseLook代码我不做修改
Update ()里初始化
  1. //这里的Pllayer是我获取的那个对象
  2. target = GameObject.Find("Player");
复制代码
下面贴出全部代码
  1. using UnityEngine;
  2. using System.Collections;

  3. [AddComponentMenu("Camera-Control/Mouse Look")]
  4. public class MouseLook : MonoBehaviour {

  5.         public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
  6.         public RotationAxes axes = RotationAxes.MouseXAndY;
  7.         public float sensitivityX = 15F;
  8.         public float sensitivityY = 15F;

  9.         public float minimumX = -360F;
  10.         public float maximumX = 360F;

  11.         public float minimumY = -85F;
  12.         public float maximumY = 4F;

  13.         public float rotationY = 0F;
  14.         
  15.         public GameObject target;
  16.         void Update ()
  17.         {
  18.                 target = GameObject.Find("Player");
  19.              if(theDistance>0)
  20.                   theDistance = 0;
  21.              if(theDistance < MaxDistance)
  22.                   theDistance = MaxDistance;
  23.                 if(Input.GetMouseButton(1))
  24.                 {
  25.                         transform.position = target.transform.position;
  26.                         if (axes == RotationAxes.MouseXAndY)
  27.                         {
  28.                                 float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
  29.                                 
  30.                                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  31.                                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
  32.                                 transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
  33.                         }
  34.                         else if (axes == RotationAxes.MouseX)
  35.                         {
  36.                                 transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
  37.                         }
  38.                         else
  39.                         {
  40.                                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  41.                                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
  42.                                 transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
  43.                         }
  44.                         SetDistance();
  45.                 }
  46.                 else
  47.             {
  48.                 transform.position = target.transform.position;
  49.                        SetDistance();
  50.             }
  51.         }
  52.         
  53.         void Start ()
  54.         {
  55.                 if (rigidbody)
  56.                 {
  57.                         rigidbody.freezeRotation = true;
  58.                         transform.position = target.transform.position;
  59.                 }
  60.         }
  61.         
  62.         //设置相机与人物之间的距离
  63.         void SetDistance()
  64.         {
  65.              transform.Translate(Vector3.forward * theDistance);
  66.         }
  67. }
复制代码

现在大家试试动下鼠标摄像机是不是以一个物体为中心点旋转了哇
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值