Unity3d 角色移动控制的几种方法

(一)角色控制器

首先要使用CharacterController组件,在物理里面,属性面板里面可以调它们的数据大小,然后就是写脚本。

创建move脚本,首先在Start()里面获得这些组件

    void Start()
    {
        car = GetComponent<CharacterController>();//获得角色控制器组件
        player = this.transform;
    }

在Update()里面写具体的实现

   void Update()
    {
        Vector3 move = Vector3.zero;
        move.x = Input.GetAxis("Horizontal") * Time.deltaTime;//获取横轴上数据段的值在x轴上移动
        move.z = Input.GetAxis("Vertical") * Time.deltaTime;//获取竖轴上数据段的值在x轴上移动
        move.y -= 3f * Time.deltaTime;
        car.Move(player.TransformDirection(move));
    }

完整代码

using UnityEngine;

public class move : MonoBehaviour
{
    private CharacterController car;
    private Transform player;
    // Start is called before the first frame update
    void Start()
    {
        car = GetComponent<CharacterController>();
        player = this.transform;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 move = Vector3.zero;
        move.x = Input.GetAxis("Horizontal") * Time.deltaTime;
        move.z = Input.GetAxis("Vertical") * Time.deltaTime;
        move.y -= 3f * Time.deltaTime;
        car.Move(player.TransformDirection(move));
    }
}

(二)Translate控制

完整代码

using UnityEngine;

public class move : MonoBehaviour
{
    private Transform player;
    // Start is called before the first frame update
    void Start()
    {
        player = this.transform;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * Time.deltaTime * 5f);//对象可以朝着世界坐标向前移动
        }
    }
}

(三)velocity移动

使用velocity来进行移动,首先必须要添加刚体Rigidbody组件,这是刚体Rigidbody的一个属性。

创建move脚本,首先在Start()里面获取刚体组件

    void Start()
    {
        player = GetComponent<Rigidbody>();
    }

在Update()里面实现移动

   void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            player.velocity = new Vector3(0, 5, 0);
        }
    }

完整代码

using UnityEngine;

public class move : MonoBehaviour
{
    private Rigidbody player;
    // Start is called before the first frame update
    void Start()
    {
        player = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            player.velocity = new Vector3(0, 5, 0);
        }
    }
}

(四)Vector3.Lerp

通过插值进行平移,可用于摄像机跟踪。

完整代码

using UnityEngine;

public class move : MonoBehaviour
{
    private Transform player;
    Vector3 toposition;//插值
    // Start is called before the first frame update
    void Start()
    {
        player = this.transform;
        toposition = new Vector3(0, 5, 0);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            player.transform.position = Vector3.Lerp(transform.position, toposition, Time.deltaTime * 5f);
        }//Vector3.Lerp(目标位置,插值,移动速度)
    }
}

谢谢!


版权声明:本文为qq_46060398原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。