Unity角色移动方式

Unity角色移动方式

  • 一、Translate控制角色移动
  • 二、Rigidbody刚体控制角色移动
  • 三、CharactorController控制角色移动
        • CharacterController用于控制第一人称或第三人称角色的运动,使用这种方式可以模拟人的一些行为,比如限制角色爬坡的最大斜度,步伐的高度等

一、Translate控制角色移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 此脚本挂载在需要控制的物体上
/// 
public class TranslateMove : MonoBehaviour
{
    private Transform player;

    private float moveSpeed = 10;

    private float horizontal;
    private float vertical;

    private 
    // Start is called before the first frame update
    void Start()
    {
        player = this.transform;
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    private void Move()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");

        if( horizontal !=0 || vertical != 0 )
        {
            player.Translate(new Vector3(horizontal,0,vertical) * Time.deltaTime * moveSpeed);
        }
    }
}        

二、Rigidbody刚体控制角色移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 此脚本挂载在需要控制的物体上
/// 
public class RigidbodyMove : MonoBehaviour
{
    private Rigidbody rigidbody;

    private float moveSpeed = 10;

    private float horizontal;
    private float vertical;
    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetObjectComponent(gameObject);//获取自身的Rigidbody组件
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");

        //VelocityMove();
        //MovePositionMove();
        AddForceMove();
    }

    /// 
    /// 给刚体赋值指定的速度,一般较适用于需要对使用刚体的物体进行细微的速度控制.
    /// 
    private void VelocityMove()
    {
        rigidbody.velocity = new Vector3(horizontal,0,vertical) * moveSpeed;
        rigidbody.gameObject.transform.rotation = Quaternion.identity;//移动时禁止旋转
    }

    /// 
    /// 让刚体移动至指定位置
    /// 
    private void MovePositionMove()
    {
        Vector3 moveOffset = new Vector3();
        moveOffset.Set(horizontal,0f,vertical);

        moveOffset = moveOffset.normalized * Time.deltaTime * moveSpeed;

        rigidbody.MovePosition(transform.position + moveOffset);
    }
	
	/// 
    /// 给刚体施加一个力
    /// 
    private void AddForceMove()
    {
        rigidbody.AddForce(new Vector3(horizontal,0,vertical) * moveSpeed);
        rigidbody.gameObject.transform.rotation = Quaternion.identity;//禁止旋转
    }

    #region 工具Function
    /// 
    /// 获取GameObject的某个组件。若没有就添加
    /// 
    /// 组件名称
    /// 需要获取某个组件的GameObject对象
    /// 需要获取的组件
    private T GetObjectComponent(GameObject obj) where T:Component
    {
        if (!obj.GetComponent())
        {
            obj.AddComponent();
        }
        return obj.GetComponent();
    }
    #endregion
}

三、CharactorController控制角色移动

CharacterController用于控制第一人称或第三人称角色的运动,使用这种方式可以模拟人的一些行为,比如限制角色爬坡的最大斜度,步伐的高度等

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// CharacterController用于控制第一人称或第三人称角色的运动,
/// 使用这种方式可以模拟人的一些行为,比如限制角色爬坡的最大斜度,步伐的高度等
/// 
public class CharactorControllerMove : MonoBehaviour
{
    private CharacterController characterController;

    private float moveSpeed = 10f;
    private float gravity = 20f;

    private float horizontal;
    private float vertical;

    // Start is called before the first frame update
    void Start()
    {
        characterController = GetObjectComponent(gameObject);
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    /// 
    /// 角色控制器 控制角色移动
    /// 
    private void Move()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");

        //不考虑重力作用
        //characterController.Move(new Vector3(horizontal,0,vertical) * Time.deltaTime *moveSpeed);

        //考虑重力作用
        Vector3 moveDirection = new Vector3(horizontal,0,vertical);
        moveDirection = transform.TransformDirection(moveDirection);//将自身坐标系转化成世界坐标系
        moveDirection *= moveSpeed;
        moveDirection.y -= gravity * Time.deltaTime;//添加重力
        //characterController.Move(moveOffset);//根据自身坐标系移动

        characterController.Move(moveDirection * Time.deltaTime);//根据世界坐标系移动
    }

    #region 工具Function
    private T GetObjectComponent(GameObject obj) where T : Component
    {
        if (!obj.GetComponent())
        {
            obj.AddComponent();
        }
        return obj.GetComponent();
    }
    #endregion
}

你可能感兴趣的:(Unity,Unity)