使用RigidBody.AddRelativeForce改变方向时的奇怪行为 [英] Strange behavior when changing direction using RigidBody.AddRelativeForce
问题描述
(暂时)我的角色是一个立方体.多维数据集使用C#脚本和刚体组件进行映射.
My character is (temporally) a cube. The cube is mapped with a C# script and Rigid Body component.
我在Update()
中使用以下代码来更改角色(是刚体)的方向:
I use the following code in Update()
to change my character (which is a rigid body)'s direction:
void Update () {
rigidbody.AddRelativeForce(transform.right * speed, ForceMode.Acceleration);
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
gameObject.transform.Rotate(0, -90, 0);
rigidbody.velocity = transform.right* rigidbody.velocity.magnitude;
}
if (Input.GetKeyDown(KeyCode.RightArrow)) {
gameObject.transform.Rotate(0, 90, 0);
rigidbody.velocity = transform.right* rigidbody.velocity.magnitude;
}
}
该行为变为: http://youtu.be/NYPNgAulc-k
我希望角色可以左右旋转90度,并开始在新方向上准确加速.但是现在,它在转弯之后会加速弯曲.有关更多信息,请参见上面的视频.
I expect the character turns 90 degree left / right and start accelerating exactly in its new direction. But now, after it turns, it accelerates in curve. See the above video for more information.
我想念什么?
更新(1月30日)::将transform.right
更改为transform.forward
,并删除了速度变化线:
UPDATE (30 Jan): Changed transform.right
to transform.forward
and remove velocity changing lines:
void Update () {
rigidbody.AddRelativeForce(transform.forward * speed, ForceMode.Acceleration);
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
gameObject.transform.Rotate(0, -90, 0);
}
if (Input.GetKeyDown(KeyCode.RightArrow)) {
gameObject.transform.Rotate(0, 90, 0);
}
}
导致向另一个方向移动(因此我更改了相机指向向量)&当向左或向右箭头时,方向不变;只有Runner对象旋转.
which results in moving in another direction (thus I change the camera pointing vector) & not changing direction when Left or Right arrow; only the Runner object rotates.
演示视频: http://youtu.be/UVG6l14oyjw
更新(2月4日):在下面应用了@Roboto的答案,并进行了一些修改:
UPDATE (4 Feb): Applied @Roboto's answer below, with some modifications made:
void Update () {
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
transform.Rotate(0, -90, 0);
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
}
if (Input.GetKeyDown(KeyCode.RightArrow)) {
transform.Rotate(0, 90, 0);
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
}
}
void FixedUpdate() {
// Physics stuff should be done inside FixedUpdate :)
rigidbody.AddForce(transform.forward * speed, ForceMode.Acceleration);
}
转弯终于可以了.我有一个额外的功能,可以使角色跳跃.在Update()
中,我添加了:
Turning is finally okay. I have an extra function that makes the character jump. In Update()
, I added:
if ((Input.GetButtonDown("Jump") || isTouched) && isOnGround) {
isOnGround = false;
rigidbody.AddForce(jumpHeight, ForceMode.VelocityChange);
}
其中isOnGround
是一个布尔值,当Runner对象接触地面时将其设置为true.但是,在离开地面之前,它会沿Z轴移动.当设备离开地面时,它会滑动到X-Z轴.已更新问题的这一部分是单独询问的:此处.
where isOnGround
is a boolean, which sets to true when the Runner object touches the ground. However, before it leaves the ground, it moves along Z axis. When the unit leaves the ground, it slides to X-Z axis. This part of updated question is asked separately: here.
注意:假设Runner对象的质量为3.
Note: Given that the Runner object has mass of 3.
推荐答案
来自文档,Transform.forward
:
The blue axis of the transform in world space.
世界空间,所以我们要使用AddForce
而不是AddRelativeForce
,因为AddRelativeForce
将使用局部坐标系来添加力.
World space, so we want to use AddForce
instead of AddRelativeForce
, since AddRelativeForce
would use the local coordinate system to add the force.
这可行:
void Update () {
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
transform.Rotate(0, -90, 0);
}
if (Input.GetKeyDown(KeyCode.RightArrow)) {
transform.Rotate(0, 90, 0);
}
}
void FixedUpdate() {
// Physics stuff should be done inside FixedUpdate :)
rigidbody.AddForce(transform.forward * speed, ForceMode.Acceleration);
}
这篇关于使用RigidBody.AddRelativeForce改变方向时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!