首先展示效果
unity3d
關(guān)于人物移動(dòng),這里推薦使用Character Conrroller組件,優(yōu)點(diǎn)就是可以不用處理剛體,不受重力的影響,自帶物理碰撞檢測(cè)。
?人物移動(dòng)代碼包括轉(zhuǎn)向
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
CharacterController characterController;
Animator animator;
private float speed;
public float walkspeed;
public float runspeed;
private bool isrun;
public Transform cam;
private Vector3 direction;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
void Start()
{
characterController = GetComponent<CharacterController>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//獲取按鍵
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
if (horizontal!=0||vertical!=0)
{
animator.SetBool("Walk", true);
}
else
{
animator.SetBool("Walk", false);
}
if(Input.GetKey(KeyCode.LeftShift))
{
animator.SetBool("Run", true);
}
else
{
animator.SetBool("Run", false);
}
isrun=Input.GetKey(KeyCode.LeftShift);
if(isrun)
{
speed = runspeed;
}
else
{
speed = walkspeed;
}
direction =new Vector3(horizontal,0f,vertical);
if (direction.magnitude>=0.1f)
{
float targetangle=Mathf.Atan2(direction.x,direction.z)*Mathf.Rad2Deg+cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetangle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation=Quaternion.Euler(0f,angle,0f);
Vector3 moveDir = Quaternion.Euler(0f, targetangle, 0f) * Vector3.forward;
characterController.Move(moveDir*speed* Time.deltaTime);
}
}
}
接下來(lái)就是攝影機(jī)的調(diào)整,這里使用unity自帶的插件cinemachine,非常的推薦,無(wú)需使用冗雜的代碼即可設(shè)計(jì)較好的攝像機(jī)跟隨視角。
安裝包:
工具欄:Window->Package Manager
安裝即可?
這里我們將使用FreeLook Camera,然后綁定角色:
比較重要的設(shè)置:
? ? ? ? ?TopRig MiddleRig BottomRig 這些是設(shè)置相機(jī)軌道也就基本定義了相機(jī),上圖數(shù)據(jù)構(gòu)成的試圖比較推薦大家試試,一定要記得改Binding Mode為 World Space。
大家可能會(huì)遇到游戲玩家的運(yùn)動(dòng)方向與按鍵方向相反的狀況,這里只需要將X Axis 中畫紅線部分取消勾選,上方Y(jié) Axis 勾選即可
?攝影機(jī)完成代碼(掛載到Main Camera上):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-505406.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersoncam : MonoBehaviour
{
public Transform orientation;
public Transform player;
public Transform playerObj;
public float rotationSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 Dir = player.position - new Vector3(transform.position.x, player.position.y, transform.position.z);
orientation.forward = Dir.normalized;
//獲取按鍵
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 inputDir = orientation.forward * vertical + orientation.right * horizontal;
if (inputDir != Vector3.zero)
{
playerObj.forward = Vector3.Slerp(playerObj.forward, inputDir.normalized, Time.deltaTime * rotationSpeed);
}
}
}
此處的playerobj以及orientation是掛載在游戲角色上的子空物體,用來(lái)輔助方向。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-505406.html
到了這里,關(guān)于unity3d 實(shí)現(xiàn)第三人稱移動(dòng)與攝像機(jī)調(diào)整的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!