- Home /
Make an object increase speed when entering trigger
Hello chaps,
I have a trigger box set up (named Trigger Zone 1) and I wish to increase the speed of my player character upon entering the trigger, and every time it does so. So it's sort of a persistent powerup.
I'm still a scripting noob (Javascript)but I tried attaching this script to my trigger:
var target : Collider;
function OnTriggerEnter (other : Collider) {
rigidbody.AddForce (0, -10, 0);
}
I get an error:
MissingComponentException: There is no 'Rigidbody' attached to the "Trigger Zone 1" game object, but a script is trying to access it.
saying my trigger does not have a rigid.body (which it doesn't, but I thought I was referencing a rigidbody that enters the trigger, not the trigger itself).
I am obviously doing something fundamentally wrong here. tried searching but just got myself confused.
Any help appreciated.
Answer by TonyLi · May 17, 2013 at 03:42 PM
You want the rigidbody of the "other" object (i.e., the player character):
function OnTriggerEnter (other : Collider) {
other.rigidbody.AddForce (0, -10, 0);
}
You might also want to add a check that other is really the player character and not some other object. Although that might be interesting if other things like bullets entered the trigger and sped up, too.
Thanks TonyLi, that worked! The trigger now acts upon the player. And yes, it might be interesting to have other things speed up in that zone, such as enemies chasing the player.
And thanks robertbu also for sending me in the right direction.
Answer by robertbu · May 17, 2013 at 03:38 PM
You are trying to add force to the trigger object, not the game object that hits the trigger. Try:
other.attachedRigidbody.AddForce (0, -10, 0);