Unity控制角色移动方式*
动态”是游戏最基本的特性之一,游戏只有动起来才能吸引人。今天主要和大家分享一下我平时通过unity控制主角移动的方式。
1
直接更改角色的属性(不建议使用,移动有卡顿):
public class MoveControl1 : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
speed = 1f;
}
// Update is called once per frame
void Update () {
if (())
{
+= new Vector3(0.1f, 0, 0);
}
else if (())
{
+= new Vector3(-0.1f, 0, 0);
}
else if (())
{
+= new Vector3(0, 0, 0.1f);
}
else if (())
{
+= new Vector3(0, 0, -0.1f);
}
}
}
2
使用方法:
public class MoveControl1 : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
speed = 1f;
}
// Update is called once per frame
void Update () {
float vertical = ("Vertical");
float horizontal = ("Horizontal");
(new Vector3(horizontal, 0, vertical) * * speed);//注意参数,只传入一个vector是不行的
}
}
3
使用刚体组件:(注意区别:此方法带有物理特性,在抬起按键后会继续移动一点距离)
public class MoveControl2 : MonoBehaviour {
// Use this for initialization
private Rigidbody rigidbody;
private float speed;
void Start () {
speed = 1f;
rigidbody = <Rigidbody>();
}
// Update is called once per frame
void Update () {
float vertical = ("Vertical");
float horizontal = ("Horizontal");
if (())
{
= * speed;
}
if (())
{
= * speed;
}
if (())
{
= * speed;
}
if (())
{
= * speed;
}
}
}
4
使用组件CharacterController:
public class MoveControl3 : MonoBehaviour {
private CharacterController character;
private float speed;
void Start () {
character = <CharacterController>();
speed = 1f;
}
// Update is called once per frame
void Update () {
MoveControl();
}
void MoveControl()
{
float horizontal = ("Horizontal"); //A D 左右
float vertical = ("Vertical"); //W S 上 下
(new Vector3(horizontal, 0, vertical) * speed * );
}
}
5
使用,movetowards,等方法常用于实现移动到固定位置,代码可自行尝试,比较简单
由于篇幅有限,今天就和大家暂时分享这些,除了上述的移动方法外,还可以使用各种好用的插件,包括easy touch,dotween等。