【发布时间】:2017-08-30 17:07:24
【问题描述】:
我想在 3D 游戏中创建推墙。这是一个在超级马里奥 64 中推墙的例子
Pushing Walls in Super Mario 64 (Video on Youtube)
这就是我的墙在检查器中的样子
这是贴在推墙上的代码
public class PushingObstacle : MonoBehaviour {
private Vector3 startPoint; // the current position when loading the scene
[SerializeField]
private Vector3 endPoint; // the target point
[SerializeField]
private float forwardSpeed; // speed when moving to the end
[SerializeField]
private float backwardSpeed; // speed when moving back to start
float currentSpeed; // the current speed (forward/backward)
private Vector3 direction; // the direction the wall is moving
private Vector3 destination; // the target point
private Rigidbody obstacleRigid; // rigidbody of the wall
private void Start()
{
startPoint = transform.position;
obstacleRigid = GetComponent<Rigidbody>();
SetDestination(endPoint); // set the target point
}
private void FixedUpdate()
{
obstacleRigid.MovePosition(transform.position + direction * currentSpeed * Time.fixedDeltaTime); // start moving
if (Vector3.Distance(obstacleRigid.position, destination) < currentSpeed * Time.fixedDeltaTime) // set a new target point
SetDestination(destination == startPoint ? endPoint : startPoint);
}
private void SetDestination(Vector3 destinationPoint)
{
destination = destinationPoint;
direction = (destination - transform.position).normalized; // set the movement direction
currentSpeed = destination == endPoint ? forwardSpeed : backwardSpeed; // set the speed
}
}
所以当玩家移动到墙壁并稍微触摸它时,墙壁就会飞走。
我可以将墙壁刚体设置为运动学模式。但我希望墙在推动玩家时给玩家增加最小的力量。我怎样才能实现这两种行为(不飞走和对玩家的小力量)?
编辑:
而且我不能将其设置为运动学,因为在墙(立方体)顶部跳跃时,玩家不会相对于墙移动。
【问题讨论】:
-
你可以增加墙的刚体质量
-
玩家的质量与墙的质量是多少?
-
他们都得到了默认质量
-
正如@Lestat 所说,尝试将墙的质量更改为更高的值,或降低玩家的质量。