👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:Unity基础实战
⭐🅰️⭐
文章目录
- ⭐🅰️⭐
- ⭐前言⭐
- 🎶(==1==) 挂载于父对象上进行跟随
- 🎶(==2==)位置定点跟随,滑轮改变视野
- 🎶(==3==) 距离差值进行跟随
- 🎶(==4==) LookAt上帝视角的跟随
- 🎶(==5==)相机的第一/三人称跟随(添加了跟随点)
- 🎶(==6==)相机 Lerp差值跟随
- ⭐🅰️⭐
⭐前言⭐
🎶(1) 挂载于父对象上进行跟随
🎶(2)位置定点跟随,滑轮改变视野
😶🌫️效果:
摄像机需要实现跟随,车同步移动,旋转。并且滑动鼠标滑轮可以调节与车辆之间的摄影距离。
public class CameraFllow : MonoBehaviour
{//目标物体public Transform target;//鼠标滑轮的速度public float ScrollSpeed = 4f;//Y轴差距参数public float Ydictance = 0f; public float Ymin = 0f;public float Ymax = 4f;//Z轴差距参数public float Zdictance = 4f;public float Zmin = 4f;public float Zmax = 8f;//相机看向的角度 和最終位置public float angle = -25 ;public Vector3 lookPosition;void LateUpdate(){//Z轴和Y轴的距离和鼠标滑轮联系Ydictance += Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed;Zdictance += Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed;//設置Y軸和x轴的滚轮滑动范围Ydictance = Mathf.Clamp(Ydictance , Ymin ,Ymax );Zdictance = Mathf.Clamp(Zdictance , Zmin, Zmax );//确定好角度,四元数 * 三维向量 = 三维向量lookPosition = Quaternion.AngleAxis(angle, target .right) * -target.forward ;//更新位置transform.position = target.position + Vector3.up * Ydictance - lookPosition * Zdictance ;//更新角度transform.rotation = Quaternion.LookRotation(lookPosition);}
}
🎶(3) 距离差值进行跟随
public class CameraMove : MonoBehaviour
{public Transform target; //跟随的目标物体private Vector3 offset; //位置偏移差void Start(){offset = transform.localPosition - target.transform.localPosition;}private void FixedUpdate(){if (target){transform.rotation = target.rotation ;transform.rotation *= Quaternion.AngleAxis(-15, Vector3.left);transform.position = target.transform.localPosition + offset;}}}
🎶(4) LookAt上帝视角的跟随
public class CameraMove : MonoBehaviour
{public Transform target; //跟随的目标物体private Vector3 offset; //位置偏移差void Start(){offset = transform.localPosition - target.transform.localPosition;}private void FixedUpdate(){if (target){ transform.rotation = target.rotation ; transform.position = target.transform.localPosition + offset;transform.LookAt(target.position +Vector3 .up*3);}}}
🎶(5)相机的第一/三人称跟随(添加了跟随点)
- 为了实现相机和人物的镜头旋转保持一致,(达到相机作为子对象的效果)
- 所以只需要再父对象中添加一个跟随点作为其子对象
public class CameraMove : MonoBehaviour
{public Transform target; //跟随的目标物体private Vector3 offset; //位置偏移差void Start(){offset = transform.position - target.GetChild(0).position;//target的第一个子对象是相机的跟随点}private void FixedUpdate(){if (target){ transform.rotation = target.GetChild(0).rotation ; transform.position = target.GetChild(0).position+ offset;transform.LookAt(target.position + Vector3 .up*2.5f);///transform.rotation = target.rotation;}}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 玩家的移动
//___________创建者:___秩沅____
//_____________________________________
//-------------------------------------
public class PlayerMove : MonoBehaviour
{private float vertical;private float horizontal;private float mousePosition;private CharacterController player; //角色控制器private Vector3 moveDerictor; //移动的方向public float velocity = 2f; //移动的速度public float roVelocity = 10f;private Animator playerAnimatior;private void Awake(){player = GetComponent<CharacterController>();playerAnimatior = GetComponent<Animator>();}private void FixedUpdate(){vertical = Input.GetAxis("Vertical") ;horizontal = - Input.GetAxis("Horizontal") ;mousePosition = Input.GetAxis("Mouse X");//旋转transform.localRotation *= Quaternion.Euler(0, mousePosition * roVelocity, 0);if (vertical != 0 ||horizontal != 0){ //移动playerAnimatior.SetFloat("SpeedWS", (int)vertical);playerAnimatior.SetFloat("SpeedAD", (int)horizontal);moveDerictor = new Vector3(vertical, 0, horizontal);print(moveDerictor.normalized);/// moveDerictor = moveDerictor.normalized; //将方向变成单位向量//transform.position= transform.position + moveDerictor.normalized*Time .deltaTime ;player.SimpleMove(transform.forward * vertical );player.SimpleMove(transform.right * -horizontal);//GetComponent<Rigidbody>().MovePosition( transform.localPosition + moveDerictor * velocity * Time.deltaTime); //速度*方向 = 向量//此时物体并非跟着自己的旋转方向进行移动而是根据自身位置进行改变//(白话:无法变成FPS的第一视角进行当前视角当前前进) }}private void MouseRotation(){}}
🎶(6)相机 Lerp差值跟随
transform.position = Vector3.Lerp(transform.position, target[ChooseIndex].position, Time.deltaTime * speed);transform.LookAt(targetOb );
⭐🅰️⭐
⭐【Unityc#专题篇】之c#进阶篇】
⭐【Unityc#专题篇】之c#核心篇】
⭐【Unityc#专题篇】之c#基础篇】
⭐【Unity-c#专题篇】之c#入门篇】
⭐【Unityc#专题篇】—进阶章题单实践练习
⭐【Unityc#专题篇】—基础章题单实践练习
⭐【Unityc#专题篇】—核心章题单实践练习
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!、