x


How to Apply a script During collison?

So, I have a car game, and i have a tornado in that car game. I also have a randomflying script. What I want is to have the randomflying script to be applied to the car while the car is touching the tornado, and to have the script deactivated when the car is not touching the tornado. I know that I would Probably have to Use OnTriggerEnter..... but im not really good at js. can anyone give me an accurate script or any tips?

more ▼

asked Aug 20 '11 at 08:36 PM

sketchers1 gravatar image

sketchers1
169 14 20 28

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

3 answers: sort voted first

There is a lot of ways to do that in Unity.

I would use this:

Tag your Tornado as "Tornado".

The OnTriggerEnter function has a parameter that you have to use to check what is the collider object.

(I use C#, but just look at the LOGIC, and not the syntax)

//This should be attached to your car
void OnCollisionStay(Collision obj){
  //obj will be the Tornado, right?
if(obj.gameobject.tag == "Tornado"){
 YouCarFlyAwayHere();
  }
}

You could also use OnCollisionEnter(). But that way you maybe should implement OnCollisionExit() as well.

I hope I helped.

Good Luck.

more ▼

answered Aug 20 '11 at 10:08 PM

J.D. gravatar image

J.D.
116 1 2

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

So, I tagged the tornado as Player, and here is the script I used:

function Update () {

//This should be attached to your car

void; OnCollisionStay("Tornado");{

//obj will be the Tornado, right?

if(obj.gameobject.tag == "Player");{

info.gameObject.AddComponent("RandomFlying");

}

}

}

This gave me these errors:

Assets/Scripts/TornadoScript.js(6,1): BCE0043: Unexpected token: if.

Assets/Scripts/TornadoScript.js(7,46): BCE0044: expecting :, found ';'.

ummmmm these are the 2 errors I cant figure out??? any help?

more ▼

answered Aug 25 '11 at 02:55 AM

sketchers1 gravatar image

sketchers1
169 14 20 28

As I said, this is just the Logic. And is in C#. Your script has .js extention.

Just Understand what i meant in code, and write in Javascript. Its really easy, if you read carefully.

Good luck!

Aug 26 '11 at 06:04 PM J.D.
(comments are locked)
10|3000 characters needed characters left

Here try this.

void OnTriggerEnter (Collider info) {
    if(info.gameObject.tag == "Car")
    {
        info.gameObject.AddComponent("RandomFlying");
    }
}

void OnTriggerExit (Collider info) {
    if(info.gameObject.tag == "Car")
    {
        info.gameObject.GetComponent<RandomFlying>().Destroy();
    }
}

There we go, now all you need to do, is to set your tornado's collider to isTrigger. Then give your car, the Car tag, and your all set :)

more ▼

answered Aug 21 '11 at 05:50 AM

diddykonga gravatar image

diddykonga
159 8 12 14

I tried this but it gave me tons of errors that i couldnt figure out how to fix. :(

Aug 21 '11 at 05:54 AM sketchers1
(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:

x2492
x342
x336
x71
x44

asked: Aug 20 '11 at 08:36 PM

Seen: 501 times

Last Updated: Aug 26 '11 at 06:04 PM