今天給大家分享一個簡單的Unity第三人稱視角下玩家控制器和相機控制器的腳本編寫方法。
?
效果如下:
一、實現(xiàn)原理
主要分三部分實現(xiàn):人物旋轉(zhuǎn)、人物移動、相機旋轉(zhuǎn)。
1.人物移動:
首先獲取到人物水平和垂直移動的參數(shù):
inputH = Input.GetAxis("Horizontal");//獲取水平/垂直移動參數(shù)
inputV = Input.GetAxis("Vertical");
因為人物移動的方向跟攝像頭一致,所以需要根據(jù)攝像頭的方向軸來確定玩家的移動方向,然后移動玩家人物剛體組件:
moveDir = mCamera.TransformDirection(inputH, 0, inputV);//人物移動朝向
rigid.MovePosition(transform.position + new Vector3(moveDir.x,0,moveDir.z) * wSpeed * Time.fixedDeltaTime);//移動人物
?2.相機旋轉(zhuǎn):
首先獲取鼠標水平/垂直移動參數(shù),再使相機跟隨人物,更新旋轉(zhuǎn):
mouseX += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;//更新鼠標水平/垂直移動參數(shù)
mouseY += Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
transform.position = body.position;//相機跟隨人物移動
transform.rotation = Quaternion.Euler(mouseY, mouseX, 0);//相機旋轉(zhuǎn)
3.人物旋轉(zhuǎn):
首先由水平/垂直移動的參數(shù)計算得到旋轉(zhuǎn)角度:
float targetRotation = (Mathf.Atan2(inputH, inputV) * Mathf.Rad2Deg + mCamera.eulerAngles.y
這里注意人物旋轉(zhuǎn)要基于當下相機的方向,如果只是基于人物模型方向,會發(fā)生人物移動與人物旋轉(zhuǎn)方向不一致的情況,如下圖,人物倒退地相機前走:
得到角度后利用Mathf.SmoothDampAngle()函數(shù)較為絲滑地旋轉(zhuǎn)人物:
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref currentVelocity, smoothTime, rSpeed, Time.deltaTime);
二、物體層級
物體層級如下:
?
玩家組件掛載在人物預(yù)制體上,并創(chuàng)建RigidBody和Collider等組件。再創(chuàng)建一個空物體,坐標與玩家人物相同,將MainCamera作為子物體,并調(diào)節(jié)相機位置。
三、完整代碼
PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody rigid;//玩家剛體
private Animator animator;//玩家動畫機
public Transform mCamera;//相機組件
private Vector3 moveDir;//人物移動方向
private float currentVelocity = 1;//目前的轉(zhuǎn)向速度(SmoothDampAngle函數(shù)的返還參數(shù))
private float smoothTime = 0.1f;//完成平滑的時間(SmoothDampAngle函數(shù)的參數(shù))
public float wSpeed;//移動速度
public float rSpeed;//旋轉(zhuǎn)速度
public float jPower;//跳躍力度
private float inputH;//水平移動參數(shù)
private float inputV;//垂直移動參數(shù)
private bool isMove;//是否移動
private bool isRun;//是否奔跑
private bool isGround;//是否在地面上
void Start()
{
rigid = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}
void Update()
{
}
private void FixedUpdate()
{
inputH = Input.GetAxis("Horizontal");//獲取水平/垂直移動參數(shù)
inputV = Input.GetAxis("Vertical");
animator.SetFloat("inputH", inputH);//更新動畫機參數(shù)
animator.SetFloat("inputV", Mathf.Abs(inputV));
animator.SetBool("isMove", isMove);
animator.SetBool("isRun", isRun);
if(Input.GetKeyDown(KeyCode.Space))
{
isGround = false;
animator.CrossFade("Jump", 0.1f);
rigid.AddForce(0, jPower, 0);//給剛體向上的力
}//更新跳躍動畫
if (inputH != 0 || inputV != 0)
{
isMove = true;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, (Mathf.Atan2(inputH, inputV) * Mathf.Rad2Deg + mCamera.eulerAngles.y), ref currentVelocity, smoothTime, rSpeed, Time.deltaTime);
}//改變?nèi)宋锍? else
{
isMove = false;
}//更新移動狀態(tài)
if (Input.GetKey(KeyCode.LeftShift))
{
wSpeed = 6;
isRun = true;
}//更新奔跑狀態(tài)
else
{
wSpeed = 3;
isRun = false;
}
moveDir = mCamera.TransformDirection(inputH, 0, inputV);
rigid.MovePosition(transform.position + new Vector3(moveDir.x,0,moveDir.z) * wSpeed * Time.fixedDeltaTime);//移動人物
}
private void OnCollisionEnter(Collision collision)
{
if(!isGround)
{
if(collision.gameObject.CompareTag("Ground"))
{
isGround = true;
}
}//判斷是否接地
}
}
CameraController.cs文章來源:http://www.zghlxwxcb.cn/news/detail-457778.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform body;//玩家人物組件
public float maxY = 40;//相機旋轉(zhuǎn)上界限
public float minY = -10;//相機旋轉(zhuǎn)下界限
public float xSpeed = 250;//相機縱向旋轉(zhuǎn)速度
public float ySpeed = 125;//相機橫向旋轉(zhuǎn)速度
private float mouseX;//鼠標水平移動參數(shù)
private float mouseY;//鼠標垂直移動參數(shù)
void Start()
{
}
void Update()
{
mouseX += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;//更新鼠標水平/垂直移動參數(shù)
mouseY += Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
mouseY = clampAngle(mouseY);
transform.position = body.position;//相機跟隨人物移動
transform.rotation = Quaternion.Euler(mouseY, mouseX, 0);//相機旋轉(zhuǎn)
}
private float clampAngle(float angle)
{
if (angle < -360)
{
angle += 360;
}
if (angle > 360)
{
angle -= 360;
}
return Mathf.Clamp(angle, minY, maxY);
}//控制相機上下旋轉(zhuǎn)角度
}
歡迎大家在評論區(qū)交流學(xué)習(xí)~文章來源地址http://www.zghlxwxcb.cn/news/detail-457778.html
到了這里,關(guān)于Unity 第三人稱 玩家控制器+相機控制器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!