当前位置:   article > 正文

Unity_相机灵活跟随角色移动_unity相机跟随角色

unity相机跟随角色

每日一句:慢慢改变,慢慢成长,慢慢适应,慢慢优秀

目录

角色旋转、移动类

相机跟随人物移动类


角色旋转、移动类

/*旋转刚体,位移的动画驱动移动*/

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerMoement : MonoBehaviour
  5. {
  6.    
  7.     public float turnSmooting = 15f;
  8.     public float speedDampTime = 0.1f;
  9.     private Animator animator;
  10.   
  11.     void Start()
  12.     {
  13.         animator = GetComponent<Animator>();
  14.      
  15.     }
  16.    
  17.     //不受帧率影响,有物理计算的时候使用,不会因为帧率影响卡帧
  18.     private void FixedUpdate()
  19.     {
  20.         float h = Input.GetAxis("Horizontal");
  21.         float v = Input.GetAxis("Vertical");
  22.         MovementManagement(h, v);
  23.     }
  24.     //旋转方法,参考绝对坐标系
  25.     void Ratating(float h, float v)//(横轴的值,纵轴的值)
  26.     {
  27.         //按下横轴和纵轴的键,前左、前右,后左、后右,向量,player朝向的目标向量
  28.         Vector3 targetDir = new Vector3(h, 0, v);
  29.         Quaternion targetRotation = Quaternion.LookRotation(targetDir);//绝对的旋转量,旋转刚体,位移的动画驱动移动
  30.         Rigidbody rb = GetComponent<Rigidbody>();
  31.         Quaternion newRotation = Quaternion.Lerp(rb.rotation, targetRotation, turnSmooting * Time.deltaTime);
  32.         4(newRotation);
  33.     }
  34.     void MovementManagement(float h, float v)
  35.     {      
  36.         if (h != 0 || v != 0)//如果按先后左右键了
  37.         {
  38.             Ratating(h, v);
  39.             animator.SetBool("Walk", true);//插值
  40.         }
  41.         else
  42.         {
  43.             animator.SetBool("Walk", false);
  44.         }
  45.     }
  46. }

相机跟随人物移动类

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraMovement : MonoBehaviour
  5. {
  6.     //用插值方法移动
  7.     float smooth = 1.5f;
  8.     Transform player;
  9.     Vector3 playerToCamera;
  10.     float playerToCameraMag;
  11.     Vector3 newPos;    //计算的结果
  12.     // Start is called before the first frame update
  13.     void Start()
  14.     {
  15.         player = GameObject.FindWithTag(Tags.Player).transform;
  16.         playerToCamera = transform.position - player.position;//a-b等到b指向a的向量
  17.         playerToCameraMag = playerToCamera.magnitude;//计算它的模
  18.     }
  19.     //射线检测,看击中的是不是player
  20.     bool ViewingPositionCheck(Vector3 checkPos)
  21.     {
  22.         RaycastHit hit;
  23.         if (Physics.Raycast(checkPos, player.position - checkPos, out hit, playerToCameraMag))//检测点到plyer的方向
  24.         {
  25.             if(hit.transform!=player)
  26.             {
  27.                 return false;
  28.             }
  29.         }
  30.         newPos = checkPos;
  31.         return true;
  32.     }
  33.     void SmoothLookAt()
  34.     {
  35.         Vector3 camDirction = player.position - transform.position;//摄像机指向player的向量
  36.         Quaternion lookAtRotation = Quaternion.LookRotation(camDirction);
  37.         //将方向转化为旋转角度:
  38.         //传入一个方向将返回一个旋转角,当某个物体被施加这个旋转角后,这个物体的forward方向将指向传入的方向。
  39.         transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
  40.     }
  41.     private void FixedUpdate()
  42.     {
  43.         Vector3 standardPos = player.position + playerToCamera;
  44.         Vector3 abovePos = player.position + Vector3.up * playerToCameraMag;//0,1,0上方高度
  45.         Vector3[] checkPoints = new Vector3[5];
  46.         checkPoints[0] = standardPos;
  47.         checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
  48.         checkPoints[2]= Vector3.Lerp(standardPos, abovePos, 0.5f);
  49.         checkPoints[3]= Vector3.Lerp(standardPos, abovePos, 0.75f);
  50.         checkPoints[4] = abovePos; 
  51.         for(int i=0;i<checkPoints.Length;i++)
  52.         {
  53.             if(ViewingPositionCheck(checkPoints[i]))
  54.             {
  55.                 break;//每次传过来0到4 的点,之后进行射线检测,break 出来,找到了一个合适的点,newPos里
  56.             }
  57.         }
  58.         transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
  59.         SmoothLookAt();
  60.     }
  61. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/99348
推荐阅读
相关标签
  

闽ICP备14008679号