boosters on triggers

hey guys im new to unity and scripting :slight_smile: so i was wondering if anyone knew a script that when you enter a trigger you get boosted forward? because i want both of my cars (player one and player two) to be able to be boosted as well :slight_smile:

a script would be helpful as i’m a visual learner :slight_smile:

thanks, daniel (turtle)

Something like this (C#):

Put this code in a game object that has a Collider set to IsTrigger=True. When another Collider enter this trigger collider, Unity calls the OnTriggerEnter function. Also, the gameobject of the “otherCollider” is supposed to have a RigidBody component.

public float _Force;
void OnTriggerEnter (Collider otherCollider)
{
    otherCollider.rigidbody.AddForce(new Vector3(0,_Force,0));
}

hey zelk thanks but the code console says “a namespace can only contain types and namespace declarations” . this came up for lines 1 and 2 ???

thanks, Dan

Do you use my code in a .js file, then it wont work. If you use it in a .cs file, you must add the class declaration etc.

I will try to rewrite to javascript but it’s not my thing so it might get wrong:

var _Force : float;
OnTriggerEnter(otherCollider : Collider)
{
otherCollider.rigidbody.AddForce(Vector3(0,_Force,0));
}

hey zelk sorry, im not used to c# and i didn’t realise you had to do the whole class decloration ahahh soz

so the script works now but when the collider comes into contact with the ramp it just goes over it like usual. i have the collider as a rigidbody but it is kinematic and i have the ramp as a rigidbody and it is kinematic so i dont know what is going on heh and i have both set as triggers.

thanks dan

the OTHER collider must have a rigidbody.

also, make sure that you have high enough values for _Force. Try several thousands to begin with.

hmmm i think my problem might be that there is more than one collider bcos there are wheels on my car :slight_smile: im sorry i have no idea what im doin ahahah