2015-04-16 34 views
4

我想用另一個對象來捕捉對象。然後將其帶到2D中的第一個對象的起始點。 我把一個物體以恆定的速度拋向另一個物體。 第一個與第二個碰撞,之後我定義了第一個到第二個對象的倒退速度。碰撞發生時(onCollisonListener)它不會自然迴轉,所以回頭的角度是錯誤的。如何將對象粘到另一個對象上,並且這些回到Unity的起點

他們隨機隨地去。我該如何做到這一點,以便正確地返回功能? 我可以使用哪種功能?我只使用velocity.x和velocity.y 感謝您閱讀我的問題並尋求幫助, Sincerely Yours。

+1

如果您仍然需要一些幫助,您可以發佈「反向速度」計算代碼嗎?最新發生的截圖也會很好。 – Utamaru

+0

你應該更精確地知道你需要什麼。你想要第二個物體附着固定(如卡住)或更柔軟(就像一個字符串)?您可以重新使用它或使用物理鏈接。 另外,如果您使用Rigidbodies,則非常重要。 – Tom

回答

0

僞代碼:

public class Catcher : MonoBehaviour 
{ 
    Vector2 startingPoint; 

    void Awake() 
    { 
     startingPoint = transform.position; 
    } 

    void Update() 
    { 
     //Your moving code 
    } 

    void OnTriggerEnter(Collider other) 
    { 
     if(other.tag == "someTag") 
     { 
      other.transform.parent = transform; 
      StartCoroutine("MoveBack"); 
     } 
    } 

    private IEnumerator MoveBack() 
    { 
     while(transform.position != startingPoint) 
     { 
      // Move this towards starting point 
      // Use Vector2 functions like Lerp, MoveTowards or SmoothDamp 
      // Could also use Mathf functions 
      yield null; 
     } 
    } 
} 

也許增加一個標誌/檢查知道如果物體正在後退或前進。

相關問題