👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:Unity基础实战
😶🌫️效果:
摄像机需要实现跟随,车同步移动,旋转。并且滑动鼠标滑轮可以调节与车辆之间的摄影距离。
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);
}
}
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;
}
}
}
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);
}
}
}
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().MovePosition( transform.localPosition + moveDerictor * velocity * Time.deltaTime); //速度*方向 = 向量
//此时物体并非跟着自己的旋转方向进行移动而是根据自身位置进行改变
//(白话:无法变成FPS的第一视角进行当前视角当前前进)
}
}
private void MouseRotation()
{
}
}
transform.position = Vector3.Lerp(transform.position, target[ChooseIndex].position, Time.deltaTime * speed);
transform.LookAt(targetOb );
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!、