我的 2d 專案有一個門戶,我只是在玩家進入時將玩家的 transform.position 更改為退出。我希望玩家在從傳送門出來時保持它的動量。
就像它落入傳送門一樣,它應該以相同的力從傳送門的末端退出,但與傳送門末端的角度相同。
這是可能的還是我的方法錯誤?
uj5u.com熱心網友回復:
除了你可以簡單地測驗之外,在處理物理時你根本不想做任何事情Transform。
所以假設你有
Rigidbody2d playerTransform portalATransform portalB
那么我可能會做類似的事情
public static class Rigidbody2dExtensions
{
public static void Teleport(this Rigidbody2d player, Transform from, Transform to)
{
// position including current offset to portal
var localOffset = from.InverseTransformPoint(player.position);
player.position = to.TransformPoint(localOffset);
// and then apply the rotation delta
var rotationDelta = Vector2.SignedAngle(from.up, to.up);
// a) to the rotation itself
player.rotation = rotationDelta;
// b) to the direction of the velocity
player.velocity = Quaternion.Euler(0,0, rotationDelta) * (Vector3) player.velocity;
}
}
第一部分
- 通過
Rigidbody2d而不是Transform組件設定位置。 - 允許使用偏移量,因此玩家不需要被精確地傳送到目標傳送門。例如,如果您使用對撞機,它可能只與左邊緣接觸門戶,因此即使目標門戶旋轉,它也會保持此偏移。
然后是評論說的第二部分
- 根據傳送門方向旋轉玩家
- 將此增量旋轉也應用于
velocity并因此保持動量但轉換為新的門戶方向
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/520005.html
標籤:C#unity3d
