c# - 减速时如何继续移动
问题描述
我正在创建一个设置为零重力的游戏。我正在使用可以在此处找到的脚本https://github.com/brihernandez/ArcadeSpaceFlightExample
移动由油门控制,可以使用鼠标滚轮或“w”和“s”键增加/减少。
此代码控制鼠标滚轮,与“w”和“s”键相同。
private void UpdateMouseWheelThrottle()
{
throttle += Input.GetAxis("Mouse ScrollWheel");
throttle = Mathf.Clamp(throttle, 0.0f, 1.0f);
}
当我减小油门时,被油门加速的物体停止移动,并且在零重力下不会漂移或继续移动。
这是控制物理的代码。
public class PlayerPhysics: MonoBehaviour
{
[Tooltip("X: Lateral thrust\nY: Vertical thrust\nZ: Longitudinal Thrust")]
public Vector3 linearForce = new Vector3(100.0f, 100.0f, 100.0f);
[Tooltip("X: Pitch\nY: Yaw\nZ: Roll")]
public Vector3 angularForce = new Vector3(100.0f, 100.0f, 100.0f);
[Range(0.0f, 1.0f)]
[Tooltip("Multiplier for longitudinal thrust when reverse thrust is requested.")]
public float reverseMultiplier = 1.0f;
[Tooltip("Multiplier for all forces. Can be used to keep force numbers smaller and more readable.")]
public float forceMultiplier = 100.0f;
public Rigidbody Rigidbody { get { return rbody; } }
private Vector3 appliedLinearForce = Vector3.zero;
private Vector3 appliedAngularForce = Vector3.zero;
private Rigidbody rbody;
private Player player;
void Awake()
{
rbody = GetComponent<Rigidbody>();
if (rbody == null)
{
Debug.LogWarning(name + ": ShipPhysics has no rigidbody.");
}
player = GetComponent<Player>();
}
void FixedUpdate()
{
if (rbody != null)
{
rbody.AddRelativeForce(appliedLinearForce * forceMultiplier, ForceMode.Force);
rbody.AddRelativeTorque(appliedAngularForce * forceMultiplier, ForceMode.Force);
}
}
public void SetPhysicsInput(Vector3 linearInput, Vector3 angularInput)
{
appliedLinearForce = MultiplyByComponent(linearInput, linearForce);
appliedAngularForce = MultiplyByComponent(angularInput, angularForce);
}
private Vector3 MultiplyByComponent(Vector3 a, Vector3 b)
{
Vector3 ret;
ret.x = a.x * b.x;
ret.y = a.y * b.y;
ret.z = a.z * b.z;
return ret;
}
}
这是实例化两者的代码。
public class Player: MonoBehaviour
{
public static Player Playerdrone { get { return player; } }
private static Player player;
private PlayerInput input;
private PlayerPhysics physics;
public Vector3 Velocity { get { return physics.Rigidbody.velocity; } }
public float Throttle { get { return input.throttle; } }
// Start is called before the first frame update
void Awake()
{
input = GetComponent<PlayerInput>();
physics = GetComponent<PlayerPhysics>();
}
// Update is called once per frame
void Update()
{
physics.SetPhysicsInput(new Vector3(input.strafe, 0.0f, input.throttle), new Vector3(input.pitch, input.yaw, input.roll));
}
}
当我降低油门时,我无法让 Rigidboy 继续漂移,我的选择是放弃油门概念并使用简单的“Addforce”,但我更愿意保留它。
我已经编辑了脚本,使其具有与使用“w”和“s”的“滚动”类似的功能,但可以向前和向后移动我。当我放开输入时,我仍然发现自己在减速。
由于鼠标跟随功能,降低阻力和角度会导致对象旋转失控。鼠标不会使相机居中,而是使相机保持旋转并跟踪鼠标。更改鼠标脚本将导致它自己的问题,因为它控制对象的俯仰和偏航。
private void SetStickCommandsUsingMouse()
{
Vector3 mousePos = Input.mousePosition;
// Figure out most position relative to center of screen.
// (0, 0) is center, (-1, -1) is bottom left, (1, 1) is top right.
pitch = (mousePos.y - (Screen.height * 0.5f)) / (Screen.height * 0.5f);
yaw = (mousePos.x - (Screen.width * 0.5f)) / (Screen.width * 0.5f);
// Make sure the values don't exceed limits.
pitch = -Mathf.Clamp(pitch, -1.0f, 1.0f);
yaw = Mathf.Clamp(yaw, -1.0f, 1.0f);
}
解决方案
当我减小油门时,被油门加速的物体停止移动,并且在零重力下不会漂移或继续移动。
Rigidbody 组件具有可以在 Inspector 中更改的属性(在此处记录)(也可以通过脚本(在此处记录)):
- 质量:物体的质量(默认为千克)。
- 阻力:当从力移动时,有多少空气阻力会影响物体。0表示没有空气阻力,无穷大使物体立即停止运动。
听起来您的阻力值可能太大(?)。
尽管此示例项目使用“空间力学”(实际上没有空气阻力),但 Drag 属性仍可用于调整刚体的运动和“漂移行为”,并控制在油门值之后它将保持减速的时间下降到 0。
或者,您可以尝试使用 Add*Force() 调用的其他ForceMode参数:ForceMode.Acceleration 或 ForceMode.VelocityChange(忽略刚体的质量)。
为了让您的游戏更加“逼真”,您可以改为将 Drag 属性设置为 0(在真实空间中没有空气阻力 -> 无限漂移),并尝试通过尝试使用逆行火箭/推进器来主动减速船,方法是添加一个向后方向的力。但是,我猜这对于街机游戏来说可能是非常不切实际的。
还有项目的全局物理设置,可以在编辑 -> 项目设置 -> 物理下找到(在此处记录)。在那里,您还会发现许多重要的物理设置(例如重力)。
(查看PhysX 物理引擎的官方文档也很有趣,Unity 在内部将其用于其 3D 物理,以了解事物在引擎盖下的实际工作方式......)
推荐阅读
- jquery - jQuery toggleClass() 在浏览器中不起作用,但在小提琴中工作正常
- c++ - 分配 char 数组
- mysql - MySQL 8 获取当前使用的(默认)数据库名称
- sql - Azure 时间点还原
- php - 稍后使用“生成(随机字符)”和“完成(插入数据库)”的 HTML 表单
- c - C中的阶乘int值
- testing - 如何在工作区中的 Rust crates 之间共享测试代码?
- javascript - 从jquery中的json响应中获取当前月份的日期范围
- css - 我怎样才能把下面的弯曲元素放在圆圈的正下方?
- c++ - 为什么这个字符串实现比字符串中的字符多分配一个字符?