方法一:使用 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,并乘以移动速度speed和Time.deltaTime。Time.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)方法施加力,使球体移动。这种方法可以实现一些特殊的移动效果,如加速、减速或周期性的速度变化。

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

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

1533

被折叠的 条评论
为什么被折叠?



