x


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

more ▼

asked Feb 05, 2012 at 12:27 AM

Spilker gravatar image

Spilker
1 6 4 4

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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) <= your definition is wrong, since Collision is a system object function OnTriggerEnter (other : Collider) <= you need to use this definition instead

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!

more ▼

answered Feb 05, 2012 at 08:09 AM

roamcel gravatar image

roamcel
1.4k 106 81 90

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 05, 2012 at 08:22 AM

Aram Azhari gravatar image

Aram Azhari
454 36 38 55

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 07, 2012 at 02:59 AM

Spilker gravatar image

Spilker
1 6 4 4

which line is giving this error? (the line number is written in the console window)

Feb 07, 2012 at 06:21 AM Aram Azhari
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x8524
x1644
x263

asked: Feb 05, 2012 at 12:27 AM

Seen: 1090 times

Last Updated: Feb 07, 2012 at 06:21 AM