2
我有一個播放器控制plattform。當移動到外邊緣時,平臺開始移動。外部觸發器的寬度由代碼計算。移動父變換時子變換不移動
所以玩家可以向任何方向移動,他只需要靠近邊緣來觸發移動。
玩家得到了一個剛體連接,平臺太。這是平臺的檢查員的形象我使用
,這是附着
[SerializeField]
private float speed; // the movementSpeed
[SerializeField]
private float movementTriggerWidth; // width of the triggers at the outer edges
private Vector3 movementDirection = Vector3.zero;
private Rigidbody platformRigid;
private GameObject player;
private float triggerDistance; // distance from center to a trigger
private void Start()
{
player = Globals.GetPlayerObject(); // search for the player Object in the scene
platformRigid = GetComponent<Rigidbody>();
triggerDistance = transform.localScale.x/2 - movementTriggerWidth; // set the trigger distance
}
private void OnTriggerEnter(Collider col)
{
col.transform.parent = transform; // set the player as a child of the platform
}
private void OnTriggerExit(Collider col)
{
col.transform.parent = null; // leave the platform
}
private void OnTriggerStay(Collider col)
{
if (col.gameObject == player) // only the player can move the platform
{
Vector3 playerPosition = player.transform.position;
Vector3 platformPosition = transform.position;
if (Vector3.Distance(playerPosition, platformPosition) > triggerDistance) // player is in outer trigger?
{
movementDirection = playerPosition - platformPosition; // calculate the movement direction
platformRigid.MovePosition(transform.position + movementDirection * speed * Time.deltaTime); // move the platform
}
}
}
現在的問題代碼:
當一個平臺上跳,玩家變得平臺的孩子。但是當平臺開始移動時,玩家不會受此影響。他不會被平臺感動。
我希望有人能幫助我解決這個「小」(?)錯誤。
更新:
這裏是玩家督察
https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html - 這可能有助於 – Fiffe
ahh ok,所以我的平臺必須不是運動學的。但是當跳上非運動平臺時,它就會崩潰。即使「使用重力」停用。 – Question3r
你能分享玩家視察員的圖像嗎? – ZayedUpal