- Home /
Question by
electfall70 · Apr 30, 2014 at 02:51 PM ·
rigidbodytriggeraddforceattack
Addforce to gameobject i am colliding with
Hey i want to add force to instead of this.gameObject something like that.gameObject . So i add force to the gameobject that is tagged as box if i collide with it. feel free to clean the code if there is something wrong
#pragma strict
var attacking : boolean;
var hasAttacked : boolean;
function Update () {
Attack();
}
function Attack(){
if(Input.GetMouseButtonDown(0))
{
attacking = true;
yield WaitForSeconds(5);
attacking = false;
}
}
function OnTriggerEnter(col : Collider)
{
if(col.collider.tag == "Box" && attacking == true && hasAttacked == false)
{
that.gameObject.rigidbody.AddForce(Vector3.up * 10000);
hasAttacked = true;
}
else if(col.collider.tag == "Enemy")
{
//attack enemy
}
}
function OnTriggerExit(col : Collider)
{
if(col.collider.tag == "Box")
{
hasAttacked = false;
}
else if(col.collider.tag == "Enemy")
{
hasAttacked = false;
}
}
Comment
Answer by robertbu · Apr 30, 2014 at 02:55 PM
If you take a look at the reference page for the Collider class, you will see that it inherits a reference to the rigidbody. So in your case:
else if(col.tag == "Enemy")
{
//attack enemy
col.rigidbody.AddForce(someForce);
}
Note that 'col' is the collider, so you don't have to do 'col.collider'.