- Home /
OnTriggerEnter questions
I am fairly new to Unity and google'd around for an answer to my problem and really didn't find anything. For reference, I am using unity for a class I am taking in which the final project is a game we have to create. I am rusty on my programming as I have not written a program in about 2 years. I have experience in Java so all my scripts will be in JavaScript. Now on to my question.
What I am currently trying to create and having problems with is an onTriggerEnter script. Imagine any adventure game you have played in a cave with icicles on the ceiling. At some point in the cave you are in, the icicles fall down at you forcing you to dodge them. I want to recreate this. To simplify this, I am using a sphere created in unity as my "icicle". I set up a plane as my "trigger point". When my character walks into this plane, the ball(icicle) will fall from its resting place above me. The problem I am having is that the script only recognizes the object it is attached to, and the object it collided with...neither of which are my ball. The way I imagine handling this(I could be wrong about my approach this is just what I was thinking), is to create my ball object(icicle) that has a rigid body attached to it. The ball(icicle) is then attached to a non rigid body object(this will allow gravity to affect it, yet it still floats in the air). My character would walk into the trigger plane, and the script would then delete the game object my ball(icicle) is attached to. This would cause the ball(icicle) to fall towards the ground where my character is at.
Anyway, as I said I am having trouble getting the script to do anything to the ball or any other object that isn't the collider or the object the script is attached to(in this instance my trigger plane). I believe is has to do with scope. As in the ball object isn't defined in the scope of the script, so it has no reference to a ball game object. How would I access and force things to happen on objects outside of the script?
Thank you in advance for any help on the subject.
You'll want to use a Rigidbody and simply enable "Use Gravity" when the player steps on the trigger. For the trigger, you need to ensure that the script that is handling your trigger enter event and the collider that is the trigger both exist as components at the same level in your object hierarchy. When this is the case you will be able to either GetComponent on your handler's gameObject to find other information; or just use the variables natively assigned inside of your handler behavior.
If you haven't done the platformer tutorial yet, you really should; as all of what you're trying to do here is covered in depth in that tutorial set. It's available for free on Unity's website and it can be completed in just a few hours.
All the best.
Answer by jebey2 · Mar 08, 2012 at 04:43 PM
For the trigger, you need to ensure that the script that is handling your trigger enter event and the collider that is the trigger both exist as components at the same level in your object hierarchy. When this is the case you will be able to either GetComponent on your handler's gameObject to find other information; or just use the variables natively assigned inside of your handler behavior.
I don't understand exactly everything you mentioned here, but this is where I am having problems. I wasn't aware you can turn off gravity on a certain object and then turn it on using a script. I like that idea better than what I was planning on doing. Object hierarchy I am guessing is scope. As in, an object inside a private function cant call on an object outside of a private function and vice verse. Which would mean if the two objects are not in the same level they cant call on each other. From what I can tell they are on the same level. My testing scene I made is very simple: One plane for the ground, one plane as my tigger plane, a sphere(to act as the icicle that will fall), and a cylinder(which is my player or character). The only script I have is a script to control my characters movement. I am trying to make a second script attached to the trigger plane which will now turn on gravity on the sphere(icicle) when an object enters the trigger plane. The character will be the only thing colliding with the trigger plane, not the sphere. I know how to access information about the object colliding with the trigger, but what I want to manipulate is an object that is not colliding with the trigger plane. How would I accomplish that? Every time I try to access the sphere(icicle) it tells me it does not recognize that object.
Answer by jebey2 · Mar 08, 2012 at 04:43 PM
I figured it out! at the start of the script I created an object named target, and instantiated it like this:
target = GameObject.FindWithTag("Icicle");
I made my sphere have the tag "Icicle" and from there I just had to reference target, and BAM! I could do what I wanted. Thank you again for the useGravity suggestion. That was so much easier than what I was thinking and it worked like a charm.