- Home /
Trigger Respawn
I'm making car game, and you pull up to a intersection and you have to answer the question. I want to have it so if they get it wrong and drive the wrong way that I can use a trigger to respawn them back before the question.
function OnTriggerEnter (Collision : Collider) { transform.localPosition = Vector3(1085,3,944); transform.rotation = Quaternion.identity; transform.Rotate(Vector3.up * 0, Space.World); } How can I have it so I have that on my cube trigger and it'll take my car and not my trigger to that position
Answer by roamcel · Feb 05, 2012 at 08:09 AM
Your question is not really clear, but I believe that your problem comes from the use of the 'transform' variable.
transform (the reference of the current objects' transform), different from 'Transform' (the object class), indicates the transform property of the object the script is running from.
So if you attach the script to a cube, and type
transform.Translate(1,0,0);
the cube will translate 1 unit to world right direction.
What you need instead is to acces the 'other' object transform, which is conveniently accessible through the 'collider' parameter that the trigger functions allow.
function OnTriggerEnter (Collision : Collider)
function OnTriggerEnter (other : Collider)
inside the function then, you use
other.transform.Translate(1,0,0);
and this will move the object that entered the collider, rather than the cube the script is attached to!
Answer by Aram-Azhari · Feb 05, 2012 at 08:22 AM
Add a property to the cube trigger code: public GameObject Car; Then on the triggerEnter code: Car.transform.localPosition = Vector3(1085,3,944); Car.transform.rotation = Quaternion.identity; Car.transform.Rotate(Vector3.up * 0, Space.World); Now save the code, in the Hierarchy select the cube, and then drag and drop your car object into its inspector window (on top of Car property).
This was one way of doing it. The other way is to have your car having a unique Tag. And then use GameObject.FindWithTag("the tag you chose for the car object") in the on trigger to get the car object. So you don't have to drag drop the car object to the cube inspector anymore.
Answer by Spilker · Feb 07, 2012 at 02:59 AM
public GameObject.FindWithTag("Car");
function OnTriggerEnter (Collision : Collider) { Car.transform.localPosition = Vector3(901.2015,-0.02017058,936.0092); Car.transform.rotation = Quaternion.identity; Car.transform.Rotate(Vector3.up * 90, Space.World); } when I put that in it gives me a unexpected token: Game Object error
which line is giving this error? (the line number is written in the console window)
Your answer
Follow this Question
Related Questions
If trigger hit, spawn it 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Change skybox at runtime? 2 Answers
Getting audio.PlayOneShot to work 2 Answers
Safe area from enemies 1 Answer