Unity角色移动方式

一、Translate控制角色移动

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

/// <summary>
/// 此脚本挂载在需要控制的物体上
/// </summary>
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;

/// <summary>
/// 此脚本挂载在需要控制的物体上
/// </summary>
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<Rigidbody>(gameObject);//获取自身的Rigidbody组件
    }

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

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

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

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

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

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

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

三、CharactorController控制角色移动

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

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

/// <summary>
/// CharacterController用于控制第一人称或第三人称角色的运动,
/// 使用这种方式可以模拟人的一些行为,比如限制角色爬坡的最大斜度,步伐的高度等
/// </summary>
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<CharacterController>(gameObject);
    }

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

    /// <summary>
    /// 角色控制器 控制角色移动
    /// </summary>
    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<T>(GameObject obj) where T : Component
    {
        if (!obj.GetComponent<T>())
        {
            obj.AddComponent<T>();
        }
        return obj.GetComponent<T>();
    }
    #endregion
}

猜你喜欢

转载自blog.csdn.net/X_King_Q/article/details/118293290