Question by
Andrew-Le · Feb 21, 2016 at 08:09 PM ·
addforceontriggerenter2d
AddForce is not working OnTriggerEnter2D
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.name == "Shell")
{
if (Input.GetKey(KeyCode.X))
{
print("A");
anim.SetBool("IsHoldShell", true);
}
else
{
print("B");
anim.SetBool("IsHoldShell", false);
col.gameObject.GetComponent<BoxCollider2D>().isTrigger = false;
col.gameObject.GetComponent<Rigidbody2D>().isKinematic = false;
if (transform.rotation.y == 0)
{
col.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 500f, ForceMode2D.Impulse);
}
else
col.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.left * 500f, ForceMode2D.Impulse);
}
}
}
And this is my problem!!
col.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 500f, ForceMode2D.Impulse);
this AddForce is not working!!! any idea?
Comment
Answer by jgodfrey · Feb 21, 2016 at 11:43 PM
Is it really not working, or are you simply never getting to that line of code? If you place a Debug.Log statement just above the line you say is not working, does the statement ever print out?
If it does not (which I suspect is the case) the problem may be with this line:
if (transform.rotation.y == 0)
Due to floating point inaccuracies, it's never a good idea to do an equality check against a REAL value. You probably want something like this instead:
if (Mathf.abs(transform.rotation.y) < 0.001)
... or similar. This will ensure that the value is "close enough" to zero without requiring an equality check.