Unity3D,玩家的移动方式
一、数学位移
1.向量 + 向量
2.Translate
3.MoveTowards
二、角色控制器
1.Move
2.SimpleMove
三、刚体
1.vcelocity
2.AddForce
3.MovePosition
这里我们用枚举的方式:
选择要选用的移动类型与方式,unity效果图
上代码:
设置枚举
public enum emMoveType //三种移动的方式:数学,角色控制器,刚体
{
Mathf,
CharacterController,
Ridgidbody
}
public enum emMathf_SubType //数学移动的三种方式
{
Mathf,
Translate,
MoveTowards,
}
public enum emCharacterController_SubType //角色控制器的两种方式
{
Move,
SimpleMove,
}
public enum emRigidbody_SubType //刚体的三种方式
{
Vcelocity,
AddForce,
MovePosition
}
定义初始变量
public class Player : MonoBehaviour
{
// 定义枚举变量
public emMoveType mMoveType;
public emMathf_SubType mMathf_SubType;
public emCharacterController_SubType mCharacterController_SubType;
public emRigidbody_SubType mRigidbody_SubType;
public float mMoveSpeed; //移动速度
public float mRotateSpeed; //旋转速度
public Rigidbody mRigidbody; //刚体变量
public CharacterController mCharacterController; //角色控制器变量
> 初始化 Awake()
> ...
> 数学类型
// mMoveType = emMoveType.Mathf;
if(mMoveType == emMoveType.CharacterController) //选择类型:角色控制器
{
mCharacterController = transform.GetComponent<CharacterController>(); //获取角色控制器组件,获取不到的话,获取添加的组件--角色控制器CharacterController
if (mCharacterController == null)
{
mCharacterController = transform.gameObject.AddComponent<CharacterController>();
}
//设置基础信息
mCharacterController.center = new Vector3(0f, 0.65f, 0);
mCharacterController.radius = 0.5f;
mCharacterController.height = 1.23f;
}
刚体类型
else if (mMoveType == emMoveType.Ridgidbody) //选择类型:刚体,先获取刚体组件,获取不到的话,去添加刚体,再获取
{
mRigidbody = transform.GetComponent<Rigidbody>();
if (mRigidbody == null)
{
mRigidbody = transform.gameObject.AddComponent<Rigidbody>();
}
//这里要添加一个角色碰撞器,与刚体配合使用
CapsuleCollider capsuleCollider = transform.gameObject.AddComponent<CapsuleCollider>();
//设置信息
capsuleCollider.center = new Vector3(0f, 0.5f, 0f);
capsuleCollider.radius = 0.5f;
mRigidbody.constraints = RigidbodyConstraints.FreezeRotation;// | RigidbodyConstraints.FreezePositionY;
mRigidbody.drag = 2f; //设置阻力
}
数学类型
else if(mMoveType == emMoveType.Mathf) //选择类型:数学,只需要添加一个碰撞器
{
CapsuleCollider capsuleCollider = transform.gameObject.AddComponent<CapsuleCollider>();
capsuleCollider.center = new Vector3(0f, 0.5f, 0f);
capsuleCollider.radius = 0.5f;
}
代码运行Update():
void Update()
{
//设置unity设置的移动方式
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//当键盘按下的时候
if (h != 0 || v != 0)
{
//角色发生移动变化
//1.旋转
//2.移动
Vector3 dir = new Vector3(h, 0, v);
//设置四元数用来旋转。
Quaternion targetQ = Quaternion.LookRotation(dir, Vector3.up);
// transform.rotation = targetQ;
transform.rotation = Quaternion.Lerp(transform.rotation, targetQ, Time.deltaTime * mRotateSpeed);
switch (mMoveType)
{
case emMoveType.Mathf:
{
if (mMathf_SubType == emMathf_SubType.Mathf)
{
//向量的相加
transform.position += transform.forward * Time.deltaTime * mMoveSpeed;
}
else if (mMathf_SubType == emMathf_SubType.Translate)
{
//在平移的方向和距离上移动变换。
transform.Translate(transform.forward * Time.deltaTime * mMoveSpeed, Space.World);
}
else if (mMathf_SubType == emMathf_SubType.MoveTowards)
{
//计算当前和目标指定的点之间的位置,移动不超过maxDistanceDelta指定的距离。
transform.position = Vector3.MoveTowards(transform.position,
transform.position + transform.forward * Time.deltaTime * mMoveSpeed, Time.deltaTime * mMoveSpeed);
}
}
break;
case emMoveType.CharacterController:
if (mCharacterController_SubType == emCharacterController_SubType.Move)
{
//按帧移动,用附加的CharacterController组件来提供游戏对象的移动。
//因为这里移动不受重力影响,所以加一个向下的移动来模拟重力
mCharacterController.Move(-transform.up * Time.deltaTime * mMoveSpeed);
mCharacterController.Move(transform.forward * Time.deltaTime * mMoveSpeed);
}
if (mCharacterController_SubType == emCharacterController_SubType.SimpleMove)
{
//按秒移动,模拟重力,快速移动角色。
mCharacterController.SimpleMove(transform.forward * mMoveSpeed);
}
break;
case emMoveType.Ridgidbody:
{
if (mRigidbody_SubType == emRigidbody_SubType.Vcelocity)
{
//按秒移动,刚体的速度矢量。它表示刚体位置的变化率。
mRigidbody.velocity = transform.forward * mMoveSpeed;
}
else if (mRigidbody_SubType == emRigidbody_SubType.AddForce)
{
//按秒移动,向刚体添加一个力,这里效果表示加一个力来对抗阻力
mRigidbody.AddForce(transform.forward * mMoveSpeed);
}
else if (mRigidbody_SubType == emRigidbody_SubType.MovePosition)
{
//将运动学刚体移动到某个位置。
mRigidbody.MovePosition(transform.position + transform.forward * mMoveSpeed * Time.deltaTime);
}
}
break;
default:
break;
}
}