问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Unity技巧:六种实现物体WASD移动的方法

创作时间:
作者:
@小白创作中心

Unity技巧:六种实现物体WASD移动的方法

引用
CSDN
1.
https://blog.csdn.net/MY_future_/article/details/145962047

在Unity开发中,控制物体(如球体)的WASD移动是一个基础且常用的功能。本文将介绍六种不同的实现方法,包括使用Transform.Translate、Rigidbody.MovePosition、Rigidbody.AddForce、CharacterController、修改Rigidbody.velocity以及使用动画曲线控制移动速度。每种方法都有其特点和适用场景,开发者可以根据项目需求灵活选择。

方法一:使用 Transform.Translate

代码示例

using UnityEngine;
public class SphereMovementTranslate : MonoBehaviour
{
    public float speed = 5f;
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput) * speed * Time.deltaTime;
        transform.Translate(movement);
    }
}

原理

  • 输入获取
  • Input.GetAxis("Horizontal")Input.GetAxis("Vertical") 分别用于获取水平(A、D 键)和垂直(W、S 键)方向的输入值。这些值的范围通常在-1 到 1 之间,-1 表示向左或向后,1 表示向右或向前。
  • 移动向量计算:根据输入值创建一个 Vector3 类型的移动向量 movement,并乘以移动速度 speedTime.deltaTimeTime.deltaTime 是当前帧与上一帧之间的时间间隔,使用它可以确保球体在不同帧率下以相同的速度移动。
  • 位置更新transform.Translate(movement) 方法用于将球体的位置按照 movement 向量进行平移。

方法二:使用 Rigidbody.MovePosition

代码示例

using UnityEngine;
public class SphereMovementRigidbody : MonoBehaviour
{
    public float speed = 5f;
    private Rigidbody rb;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput) * speed * Time.fixedDeltaTime;
        rb.MovePosition(rb.position + movement);
    }
}

原理

  • 刚体组件获取:在 Start 方法中,通过 GetComponent<Rigidbody>() 获取球体的 Rigidbody 组件,以便后续对其进行操作。
  • 输入获取和移动向量计算:与 Transform.Translate 方法类似,根据输入值计算移动向量 movement,但这里使用 Time.fixedDeltaTime,因为 FixedUpdate 方法是固定时间间隔调用的,使用 Time.fixedDeltaTime 可以确保物理模拟的稳定性。
  • 位置更新rb.MovePosition(rb.position + movement) 方法用于将刚体的位置更新到新的位置,这种方法会考虑刚体的物理属性,如碰撞检测等。

方法三:使用 Rigidbody.AddForce

代码示例

using UnityEngine;
public class SphereMovementAddForce : MonoBehaviour
{
    public float force = 50f;
    private Rigidbody rb;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 forceDirection = new Vector3(horizontalInput, 0f, verticalInput);
        rb.AddForce(forceDirection * force);
    }
}

原理

  • 刚体组件获取:同样在 Start 方法中获取球体的 Rigidbody 组件。
  • 输入获取和力的方向计算:根据输入值计算力的方向 forceDirection
  • 施加力rb.AddForce(forceDirection * force) 方法用于向刚体施加一个力,这个力会使刚体产生加速度,从而实现移动。使用这种方法可以模拟物理效果,如惯性和摩擦力。

方法四:使用 CharacterController

代码示例

using UnityEngine;
public class SphereMovementCharacterController : MonoBehaviour
{
    public float speed = 5f;
    private CharacterController characterController;
    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput) * speed * Time.deltaTime;
        characterController.Move(movement);
    }
}

原理

  • 组件获取:在 Start 方法中,通过 GetComponent<CharacterController>() 获取球体上挂载的 CharacterController 组件。CharacterController 是 Unity 专门用于控制角色移动的组件,它提供了简单的移动控制功能,同时处理碰撞检测,不需要使用物理引擎。
  • 输入获取和移动向量计算:和前面的方法类似,通过 Input.GetAxis 获取输入值,计算移动向量 movement,并乘以速度和 Time.deltaTime 以确保帧率无关的移动。
  • 移动控制:使用 characterController.Move(movement) 方法来移动角色。这个方法会根据移动向量移动角色,并且自动处理与其他碰撞体的碰撞,避免角色穿过障碍物。

方法五:通过修改 Rigidbody.velocity

代码示例

using UnityEngine;
public class SphereMovementVelocity : MonoBehaviour
{
    public float speed = 5f;
    private Rigidbody rb;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput) * speed;
        rb.velocity = movement;
    }
}

原理

  • 刚体组件获取:在 Start 方法中获取球体的 Rigidbody 组件。
  • 输入获取和移动向量计算:根据输入值计算移动向量 movement,并乘以速度。这里不需要乘以 Time.deltaTime,因为 Rigidbody.velocity 表示刚体的速度,单位是米 / 秒。
  • 速度设置:通过直接设置 rb.velocity = movement 来改变刚体的速度,从而实现移动。这种方法会立即改变刚体的速度,不会产生加速或减速的物理效果,适用于需要快速响应输入的情况。

方法六:使用动画曲线控制移动速度

代码示例

using UnityEngine;
public class SphereMovementWithAnimationCurve : MonoBehaviour
{
    public float baseSpeed = 5f;
    public AnimationCurve speedCurve;
    private Rigidbody rb;
    private float timeElapsed = 0f;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        timeElapsed += Time.fixedDeltaTime;
        float currentSpeedMultiplier = speedCurve.Evaluate(timeElapsed);
        float currentSpeed = baseSpeed * currentSpeedMultiplier;
        Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput) * currentSpeed;
        rb.AddForce(movement);
    }
}

原理

  • 组件和变量初始化:在 Start 方法中获取 Rigidbody 组件,并初始化一个时间变量 timeElapsed 用于记录时间流逝。
  • 输入获取:通过 Input.GetAxis 获取水平和垂直输入值。
  • 速度曲线评估:使用 AnimationCurve 来定义一个速度随时间变化的曲线。在每一帧中,根据 timeElapsed 评估曲线,得到当前的速度乘数 currentSpeedMultiplier。将基础速度 baseSpeed 乘以这个乘数,得到当前的移动速度 currentSpeed
  • 施加力移动:根据当前速度计算移动向量 movement,并使用 rb.AddForce(movement) 方法施加力,使球体移动。这种方法可以实现一些特殊的移动效果,如加速、减速或周期性的速度变化。

以上方法使用步骤

  1. 创建脚本:在 Unity 的 Project 窗口中右键点击,选择 Create -> C# Script,输入脚本名称(如 SphereMovementTranslate),然后双击打开脚本,将上述代码复制到脚本中。
  2. 挂载脚本:将脚本挂载到球体对象上,在 Inspector 窗口中点击 Add Component,选择刚刚创建的脚本。
  3. 调整参数:在 Inspector 窗口中可以调整脚本中的公共参数(如 speedforce)。
  4. 运行游戏:点击 Unity 编辑器的播放按钮,使用 W、A、S、D 键控制球体移动。

开发者可以根据项目的具体需求和设计目标,灵活选择合适的方法。同时,在实际开发过程中,还可以进一步扩展和优化这些方法,结合更多的功能,如跳跃、旋转等,为游戏增添更多的趣味性和交互性。希望本文介绍的这些方法能帮助您在 Unity 开发中更加得心应手地控制物体的移动。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号