- Home /
OnTrigger & OnCollision not working?
Alright, so I'm building a paper airplane simulator for school, and though I'm very new to javascript, I think I'm doing alright for a beginner. My problem is, I have a script called "blowWind", which is as follows:
var power : float = 1000.0;
function OnTriggerEnter ( trigger : Collider ) {
if (trigger.gameObject.name == "paper plane") {
Debug.Log("Hit the trigger");
rigidbody.AddForce(Vector3(power,0,0));
}
}
I have this script applied to some transparent cubes I created for triggers that have a box collider, and IsTrigger is checked. When I launch my plane, it flies, but when I hit a trigger, it doesn't have power added to it. It doesn't even show up in the console with my debug statement, "Hit the trigger".
My Planeprefab is a rigidbody, with a mesh collider on it, and convex is ticked. I have tried every friggen possibility, and I have every tutorial video from Unity 3D Videos and have watched them all, he has an example video of this very procedure, only he has it print to the console and that is all.
I am very frustrated and stuck at this point, what am I doing wrong? I'd be willing to upload my .unity package if anybody is interested in checking it out, though it isn't quite finished.
Any and all help is greatly appreciated!!
EDIT
Maybe it has something to do with the Planeprefab being a (clone)? I instantiate the prefab, so maybe that has something to do with it?
I feel like it has to do with this plane prefab. I have now attached a script to the ground so that when the paper plane hits it, it loads my main menu for the game, but it isn't detecting the collision, both now have a box collider.
Both scenes are in the project build settings.
Here is the gameover.js:
function OnCollisionEnter ( myCollision : Collision ){
if(myCollision.gameObject.name == "paper plane"){
Application.LoadLevel("Menu");
}
}
Thanks for any help everybody, this is killing me.
Answer by N1ghtm4r3 · Feb 19, 2013 at 07:37 PM
Nobody?
It seems like it should be fairly straight forward, not sure what the problem could be.
I reccomend dont try to use an answer to bump the post, cause then your question wont be "unanswered"
Answer by RetepTrun · Feb 19, 2013 at 06:57 PM
I believe OntriggerEnter acts once only upon entry perhaps this is what you want if you want to give the plane one impulse.
Perhaps you script is on a parent object and the child has the collider?
Anyway I also wanted to make a script that added force to an object in a trigger. http://answers.unity3d.com/questions/140782/artificial-gravity.html Create a big cube, take off the renderer, drop rigidbodies into it, adjust rotation for where to push things
var forcetype:boolean;
var gravitytype:boolean;
function OnTriggerStay(other : Collider) {
if(forcetype){
if (other.attachedRigidbody) {
// Calculate the y-axis relative to us
var cameraRelativeRight : Vector3 = transform.TransformDirection (Vector3.up);
// Apply a force relative to the our y-axis
other.attachedRigidbody.AddForce(cameraRelativeRight * -9.81,ForceMode.Acceleration);
}
}
}
Correct, I want it to give it a little thrust, similar to a gust of wind. I'm not exactly sure how the script you posted would help wit what I'm trying to achieve. $$anonymous$$y blowWind script is attached to cubes, with the renderer removed, and it isn't a child object.
I just can't get this to work, and I'm not getting anywhere. I'm ready to pull my hair out.
Thanks for responding anyways, I greatly appreciate it.
I noticed something.
You mentioned that your plane is a clone.
You are checking gameObject.name == "paper plane"
In when you make a clone, his name is not "paper plane" it is "paper plane(clone)"
Doublecheck in inspector with game running.
(I reccomend checking using tags ins$$anonymous$$d of names in the long run)
Hey RetepTrun, thank you so much, I'm not sure why I didn't try that, I'm new to unity and javascript, but I got it working using the tag, as you suggested.
You have no idea how thankful I am!! Take care!
Your answer
Follow this Question
Related Questions
OnTrigger, OnCollison 1 Answer
Performance difference between OnCollision and OnTrigger messages? 2 Answers
i can see my objects collide but OnCollisionEnter never called c# 1 Answer
how to access a script, and affect only one of the gameobjects its attached to? 1 Answer
OnTriggerEnter and Exit Not Aligned 0 Answers