我正在使用Unity中的多人射击游戏。它使用Rigidbodies来处理武器等可互动项目。我们将该代码基于此repo的修改版本
https://github.com/TomorrowTodayLabs/NewtonVR
相关代码是
protected override void FixedUpdate()
{
base.FixedUpdate();
if (IsAttached == true)
{
Vector3 PositionDelta;
Quaternion RotationDelta;
float angle;
Vector3 axis;
if (InteractionPoint != null)
{
RotationDelta = AttachedHand.transform.rotation * Quaternion.Inverse(InteractionPoint.rotation);
PositionDelta = (AttachedHand.transform.position - InteractionPoint.position);
}
else
{
RotationDelta = PickupTransform.rotation * Quaternion.Inverse(this.transform.rotation);
PositionDelta = (PickupTransform.position - this.transform.position);
}
RotationDelta.ToAngleAxis(out angle, out axis);
if (angle > 180)
angle -= 360;
if (angle != 0)
{
Vector3 AngularTarget = (Time.fixedDeltaTime * angle * axis) * AttachedRotationMagic;
this.Rigidbody.angularVelocity = Vector3.MoveTowards(this.Rigidbody.angularVelocity, AngularTarget, 10f);
}
Vector3 VelocityTarget = PositionDelta * AttachedPositionMagic * Time.fixedDeltaTime;
this.Rigidbody.velocity = Vector3.MoveTowards(this.Rigidbody.velocity, VelocityTarget, 10f);
}
}
使用刚体的好处是可以通过墙壁,地板和其他刚体进行碰撞检测。这在多人游戏中至关重要,你不希望玩家通过墙壁推射武器和射击人来作弊。问题是它在武器和Vive控制器之间引入了很多延迟。这么难以瞄准,你需要等到刚刚抓住控制器。 有什么好办法解决这个问题并保持碰撞检测?