2017-07-14 41 views
0
工作

我有以下腳本AddForce不統一

public class SpeedUp : MonoBehaviour { 

    public Rigidbody2D ball; 

    void OnTriggerEnter2D(Collider2D col) 
    { 
     if (col.tag == "Ball") { 
      ball.AddForce(Vector2.left * 1000f); 
      Debug.Log("ABC"); 
     } 

    } 

} 

每次我跑我的比賽中,的debug.log(「ABC」)打印ABC在控制檯時間,但沒有按剛體」移動,它保持原樣。有人可以解釋我爲什麼,因爲我不明白爲什麼控制檯打印工作和剛體不動

這是球

public class Ball : MonoBehaviour { 

    public Rigidbody2D rb; 
    public Rigidbody2D hook; 
    public float releaseTime = 0.15f; 

    private bool isPressed = false; 

    void Update() 
    { 
     if (isPressed) 
     { 

      Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); 

      if (Vector3.Distance(mousePos, hook.position) > 2.5f) 
      { 
       rb.position = hook.position + (mousePos - hook.position).normalized * 2.5f; 
      } 
      else 
      { 
       rb.position = mousePos; 
      } 
     } 
    } 

    void OnMouseDown() 
    { 
     isPressed = true; 
     rb.isKinematic = true; 
    } 

    void OnMouseUp() 
    { 
     isPressed = false; 
     rb.isKinematic = false; 
     StartCoroutine(Release()); 
    } 

    IEnumerator Release() 
    { 
     yield return new WaitForSeconds(releaseTime); 
     GetComponent<SpringJoint2D>().enabled = false; 
     this.enabled = false; 
    } 
} 
+0

剛體是否標記爲運動?你能向我們展示其設置的截圖嗎? – Serlite

+0

@Serlite當鼠標沒有碰到它時,球是運動的,但爲了到達碰撞器,球必須用鼠標拉動,一旦碰到它,它就會變成動態的。 – Edward

+2

代碼沒有問題本身 - 你的問題要麼在於球的剛體如何設置,要麼與你描述的鼠標和球的相互作用。 – ryeMoss

回答

0

的剛體不動的代碼可能可它需要getComponenrt()

所以,在你的腳本中添加無效的start()方法

公共類加速:MonoBehaviour {

public Rigidbody2D ball; 

void Start() 
{ 
    ball = ball.GetComponent<Rigidbody2D>(); 
} 

void OnTriggerEnter2D(Collider2D col) 
{ 
    if (col.tag == "Ball") { 
     ball.AddForce(Vector2.left * 1000f); 
     Debug.Log("ABC"); 
    } 

} 

}