每日一句:慢慢改变,慢慢成长,慢慢适应,慢慢优秀
目录
角色旋转、移动类
相机跟随人物移动类
角色旋转、移动类
/*旋转刚体,位移的动画驱动移动*/
文章来源:https://www.toymoban.com/news/detail-713854.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMoement : MonoBehaviour
{
public float turnSmooting = 15f;
public float speedDampTime = 0.1f;
private Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
//不受帧率影响,有物理计算的时候使用,不会因为帧率影响卡帧
private void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
MovementManagement(h, v);
}
//旋转方法,参考绝对坐标系
void Ratating(float h, float v)//(横轴的值,纵轴的值)
{
//按下横轴和纵轴的键,前左、前右,后左、后右,向量,player朝向的目标向量
Vector3 targetDir = new Vector3(h, 0, v);
Quaternion targetRotation = Quaternion.LookRotation(targetDir);//绝对的旋转量,旋转刚体,位移的动画驱动移动
Rigidbody rb = GetComponent<Rigidbody>();
Quaternion newRotation = Quaternion.Lerp(rb.rotation, targetRotation, turnSmooting * Time.deltaTime);
4(newRotation);
}
void MovementManagement(float h, float v)
{
if (h != 0 || v != 0)//如果按先后左右键了
{
Ratating(h, v);
animator.SetBool("Walk", true);//插值
}
else
{
animator.SetBool("Walk", false);
}
}
}
相机跟随人物移动类
文章来源地址https://www.toymoban.com/news/detail-713854.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
//用插值方法移动
float smooth = 1.5f;
Transform player;
Vector3 playerToCamera;
float playerToCameraMag;
Vector3 newPos; //计算的结果
// Start is called before the first frame update
void Start()
{
player = GameObject.FindWithTag(Tags.Player).transform;
playerToCamera = transform.position - player.position;//a-b等到b指向a的向量
playerToCameraMag = playerToCamera.magnitude;//计算它的模
}
//射线检测,看击中的是不是player
bool ViewingPositionCheck(Vector3 checkPos)
{
RaycastHit hit;
if (Physics.Raycast(checkPos, player.position - checkPos, out hit, playerToCameraMag))//检测点到plyer的方向
{
if(hit.transform!=player)
{
return false;
}
}
newPos = checkPos;
return true;
}
void SmoothLookAt()
{
Vector3 camDirction = player.position - transform.position;//摄像机指向player的向量
Quaternion lookAtRotation = Quaternion.LookRotation(camDirction);
//将方向转化为旋转角度:
//传入一个方向将返回一个旋转角,当某个物体被施加这个旋转角后,这个物体的forward方向将指向传入的方向。
transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
}
private void FixedUpdate()
{
Vector3 standardPos = player.position + playerToCamera;
Vector3 abovePos = player.position + Vector3.up * playerToCameraMag;//0,1,0上方高度
Vector3[] checkPoints = new Vector3[5];
checkPoints[0] = standardPos;
checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
checkPoints[2]= Vector3.Lerp(standardPos, abovePos, 0.5f);
checkPoints[3]= Vector3.Lerp(standardPos, abovePos, 0.75f);
checkPoints[4] = abovePos;
for(int i=0;i<checkPoints.Length;i++)
{
if(ViewingPositionCheck(checkPoints[i]))
{
break;//每次传过来0到4 的点,之后进行射线检测,break 出来,找到了一个合适的点,newPos里
}
}
transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
SmoothLookAt();
}
}
到了这里,关于Unity_相机灵活跟随角色移动的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!