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(目标位置,插值,移动速度)
    }
}

谢谢!

  • 20
    点赞
  • 128
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值