您现在的位置是:首页 >技术教程 >Unity移动方法总结网站首页技术教程

Unity移动方法总结

ぃ咔哇依°ヽ 2023-06-15 08:00:02
简介Unity移动方法总结

1、rb.MovePosition方法

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f;
    public float smoothing = 0.1f;
    public float rotationSpeed = 5.0f;

    private Vector3 targetPosition;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        targetPosition = transform.position;
    }

    void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        Vector3 direction = new Vector3(h, 0, v).normalized;

        if (direction.magnitude >= 0.1f)
        {
            targetPosition += direction * speed * Time.deltaTime;
            Vector3 smoothedPosition = Vector3.Lerp(transform.position, targetPosition, smoothing);
            
            // 计算玩家应该面对的方向
            Vector3 lookDirection = targetPosition - transform.position;
            Quaternion targetRotation = Quaternion.LookRotation(lookDirection);

            // 平滑地旋转玩家的朝向
            rb.MoveRotation(Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime));
            rb.MovePosition(smoothedPosition);
        }
    }
}

2、rb.velocity方法

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f;
    public float rotationSpeed = 5.0f;

    private Vector3 targetPosition;
    private Quaternion targetRotation;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        targetPosition = transform.position;
        targetRotation = transform.rotation;
    }

    void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        Vector3 direction = new Vector3(h, 0, v).normalized;

        if (direction.magnitude >= 0.1f)
        {
            targetPosition += direction * speed * Time.deltaTime;
            Vector3 lookDirection = targetPosition - transform.position;
            targetRotation = Quaternion.LookRotation(lookDirection);

            rb.velocity = direction * speed;
        }
        else
        {
            rb.velocity = Vector3.zero;
        }

        rb.MoveRotation(Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime));
    }
}

3、rb.AddForce 方法:接受一个向量参数,表示要施加的力的大小和方向。

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5.0f;

    private Rigidbody rb;
    private float horizontalInput;
    private float verticalInput;

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

    void FixedUpdate()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        verticalInput = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);

        rb.AddForce(movement * moveSpeed * Time.fixedDeltaTime, ForceMode.VelocityChange);
    }
}

4、transform.Translate方法:接受一个向量参数,表示要移动的方向和距离。

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5.0f;
    public float rotateSpeed = 100.0f;

    // Update is called once per frame
    void Update()
    {
        // 获取水平和垂直输入轴的值
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        // 计算移动方向
        Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);

        // 移动游戏对象
        transform.Translate(movement * moveSpeed * Time.deltaTime);

        // 计算旋转方向
        Vector3 rotation = new Vector3(0.0f, horizontalInput, 0.0f);

        // 根据旋转方向计算旋转角度
        float rotationAngle = Vector3.SignedAngle(transform.forward, rotation, Vector3.up);

        // 计算旋转速度
        float rotationSpeed = Mathf.Clamp(Mathf.Abs(horizontalInput), 0.0f, 1.0f) * rotateSpeed;

        // 围绕游戏对象的y轴旋转
        transform.Rotate(0.0f, rotationAngle * rotationSpeed * Time.deltaTime, 0.0f);
    }
}

5、 Mathf.Lerp 函数可以平滑地将一个值从一个范围过渡到另一个范围。

//var t = 1 / ((transform.position - targetPosition).magnitude);//匀速移动
//gameObject.transform.position = Vector3.Lerp(transform.position, targetPosition, t*0.01f);

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5.0f;
    public float smoothTime = 0.3f;

    // Update is called once per frame
    void Update()
    {
        // 获取水平和垂直输入轴的值
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        // 计算移动方向
        Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);

        // 计算目标位置
        Vector3 targetPosition = transform.position + movement * moveSpeed;

        // 平滑地移动游戏对象
        transform.position = Vector3.Lerp(transform.position, targetPosition, smoothTime);
    }
}

6、 Vector3.SmoothDamp 函数:接受四个参数:current:当前值;target:目标值;currentVelocity:当前速度,这个值会在函数中被修改以实现平滑过渡;smoothTime:平滑时间,表示从当前值到目标值所需的时间。

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5.0f;
    public float smoothTime = 0.3f;

    private Vector3 velocity = Vector3.zero;

    // Update is called once per frame
    void Update()
    {
        // 获取水平和垂直输入轴的值
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        // 计算移动方向
        Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);

        // 计算目标位置
        Vector3 targetPosition = transform.position + movement * moveSpeed;

        // 平滑地移动游戏对象
        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
    }
}

7、MoveTowards 方法:接受三个参数:当前位置、目标位置和移动速度。

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5.0f;

    // Update is called once per frame
    void Update()
    {
        // 获取水平和垂直输入轴的值
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        // 计算移动方向
        Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);

        // 计算目标位置
        Vector3 targetPosition = transform.position + movement * moveSpeed;

        // 平滑地移动游戏对象
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
    }
}

8、Move 方法:接受一个向量参数,表示要移动的方向和距离。

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5.0f;

    private CharacterController controller;
    private float horizontalInput;
    private float verticalInput;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    void FixedUpdate()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        verticalInput = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);

		controller.Move(movement * moveSpeed * Time.fixedDeltaTime);
        //controller.SimpleMove(movement * moveSpeed);//接受一个向量参数,表示要移动的方向和距离,同时会自动处理重力和碰撞检测。
    }
}
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。