概述
Movement Solutions
private float speed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
MovementSolution1(h, v);
}
private void MovementSolution1(float h, float v)
{
Vector3 dir = new Vector3(h, 0, v);
rb.MovePosition(rb.position + dir * speed * Time.fixedDeltaTime);
}
private void MovementSolution2(float h, float v)
{
Vector3 movement = new Vector3(0, 0, 0);
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.fixedDeltaTime;
rb.MovePosition(rb.position + movement);
}
private void MovementSolution3(float h, float v)
{
Vector3 movement = new Vector3(h, 0f, v);
movement = movement.normalized * speed * Time.fixedDeltaTime;
transform.Translate(movement);
}
private void MovementSolution4(float h, float v)
{
Vector3 movement = new Vector3(h, 0f, v);
movement = movement.normalized * speed * Time.fixedDeltaTime;
//持续递增
rb.AddForce(movement);
}
private void MovementSolution5(float h, float v)
{
Vector3 movement = new Vector3(h, 0f, v);
movement = movement.normalized * speed * Time.fixedDeltaTime;
//持续不变
rb.velocity = movement;
}
Rotation Solution
private Rigidbody rb;
private float rotateSpeed;
private float rotateLimit;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate(){
//水平
float mouseX = Input.GetAxis("Mouse X");
//垂直
float mouseY = Input.GetAxis("Mouse Y");
RotationSolution1(mouseX, mouseY);
RotationSolution2(mouseX, mouseY);
}
private float ClampRotation(float rotation, float currentLocalRotation){
float currentRotationY = currentLocalRotation;
currentRotationY += rotation;
currentRotationY = Mathf.Clamp(currentRotationY, -rotateLimit, rotateLimit);
return currentRotationY;
}
//相机上下旋转,人物左右旋转
private void RotationSolution1(float mouseX, float mouseY){
//相机垂直旋转
camera.transform.localRotation = Quaternion.Euler(ClmapRotation(mouseY, camera.transfrom.localRotation.y), camera.transform.localRotation.x , 0);
//人物水平旋转
transform.localRotation = transform.localRotation * Quaternion.Euler(0, mouseX, 0);
}
//人物上下左右旋转
private void RotationSolution2(float mouseX, float mouseY){
//设置垂直旋转范围
float currentRotationY = transfrom.localRotation.y;
currentRotationY += mouseY;
currentRotationY = Mathf.Clamp(currentRotationY, -rotateLimit, rotateLimit);
transform.localRotation = transform.localRotation * Quaternion.Euler(0, mouseX, 0);
}
private void RotationSolution3(){
//人物左右旋转
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
//相机上下旋转
currentCameraRotationX += cameraRotationX;
currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -cameraRotationLimit,
cameraRotationLimit);
camera.transform.localEulerAngles = new Vector3(-currentCameraRotationX, 0f, 0f);
}
最后
以上就是靓丽酸奶为你收集整理的Unity3D 第三人称移动解决方法汇总Movement Solutions的全部内容,希望文章能够帮你解决Unity3D 第三人称移动解决方法汇总Movement Solutions所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复