2017-08-26 49 views
2

我有一個播放器控制plattform。當移動到外邊緣時,平臺開始移動。外部觸發器的寬度由代碼計算。移動父變換時子變換不移動

所以玩家可以向任何方向移動,他只需要靠近邊緣來觸發移動。

Platform TopDown View

玩家得到了一個剛體連接,平臺太。這是平臺的檢查員的形象我使用

Platform Inspector

,這是附着

[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 
     } 
    } 
} 

現在的問題代碼:

當一個平臺上跳,玩家變得平臺的孩子。但是當平臺開始移動時,玩家不會受此影響。他不會被平臺感動。

我希望有人能幫助我解決這個「小」(?)錯誤。

更新:

這裏是玩家督察

PlayerInspector

+0

https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html - 這可能有助於 – Fiffe

+0

ahh ok,所以我的平臺必須不是運動學的。但是當跳上非運動平臺時,它就會崩潰。即使「使用重力」停用。 – Question3r

+0

你能分享玩家視察員的圖像嗎? – ZayedUpal

回答

0

好吧,我去了一些嘗試的照片,這對我來說是一個可行的解決方案。

我刪除了剛體

,並把這個代碼

[SerializeField] 
private float speed; 

[SerializeField] 
private float movementTriggerWidth; 

private Vector3 movementDirection; 
private bool move = false; 
private GameObject player; 
private float triggerDistance; 

private void Start() 
{ 
    player = Globals.GetPlayerObject(); 
    triggerDistance = transform.localScale.x/2 - movementTriggerWidth; 
} 

private void OnTriggerEnter(Collider col) 
{ 
    col.transform.parent = transform; 
} 

private void OnTriggerExit(Collider col) 
{ 
    col.transform.parent = null; 
} 

private void OnTriggerStay(Collider col) 
{ 
    if (col.gameObject == player) 
    { 
     Vector3 playerPosition = player.transform.position; 
     Vector3 platformPosition = transform.position; 

     if (Vector3.Distance(playerPosition, platformPosition) > triggerDistance) 
     { 
      movementDirection = playerPosition - platformPosition; 
      move = true; 
     } 
    } 
} 

private void FixedUpdate() 
{ 
    if (move) 
     transform.Translate(movementDirection * speed * Time.deltaTime); 

    move = false; 
} 

現在該平臺由Translate()FixedUpdate()移動。 OnTriggerStay()只是檢查,如果平臺應該移動或不。