2017-01-23 120 views
0

我試圖在按下Input時從父對象(即像一個球)拋出一個特定的子對象。我已經讓我的球員拿起一個球並將它作爲它自己的一個小孩(並隨着它移動),但是我無法獲得投擲功能的效果。這裏是我的代碼:請注意Update函數中的'/////',那是我相信代碼會去的地方?我可能是錯的。我已經嘗試了幾件事情無濟於事。Unity C# - 從父對象中拋出一個子對象

public static float playerDistance; 
bool hasGarbage; 

// Update is called once per frame 
void Update() { 

    float h = Input.GetAxis("Horizontal"); 
    transform.Translate(h * Time.deltaTime * 5f, 0f, 5f * Time.deltaTime); 
    playerDistance = transform.localPosition.z; 


    if (hasGarbage == true) 
    { 
     if (Input.GetButtonDown("Fire1")) 
     { 
      //////////////////////////////////////////////////// 

     } 
    } 

} 

private void OnTriggerEnter(Collider other) 
{ 
    if (other.tag == "Garbage") 
    { 

     other.transform.parent = transform; 
     hasGarbage = true; 


    } 
} 

}

+2

你應該考慮建立一個[固定接頭(https://docs.unity3d.com/手動/ class-FixedJoint.html),而不是實際上在球員下方養育球。當你需要拋球時,在施加任何力量之前先斷開關節。 – Serlite

回答

相關問題