x


Making a target move when hit

I have a target that when hit sends the appropriate score to the ui, but when the target gets hit I also want it to wait for a couple seconds, then move down (which would be out of sight behind a wall), wait several more seconds and then move back up to the same position. I however don't have any idea how to make it move :(

This is the code I have so far, which waits for 3 seconds when hit, but after that I'm stumped.

function OnCollisionEnter (col : Collision) {

yield WaitForSeconds(3);

}

So what I need is code to get the initial position of the object, move it a set distance(directly down), then move it back up to the initial position(directly up). Thanks for any help!

more ▼

asked May 23 '10 at 03:28 AM

FraktuRe gravatar image

FraktuRe
3 1 1 2

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

3 answers: sort voted first

Animating the position would be the easiest. First link the object to an empty gameObject, this is done because when you animate something the object will use those exact coordinates every time it plays so if you move the object or created a duplicate it will snap to those coordinates once played, but when it's linked to another object first it zeros out the x,y,z and uses the local coordinates so you can move it if you want. Select the animated object and open the animation window, then animate it moving down, then make another animation moving from down to the up position.

var animateObject : GameObject;

function OnCollisionEnter (col : Collision) {

yield WaitForSeconds(1);
   animateObject.animation.Play("upPOS-downPOS");

yield WaitForSeconds(3);
   animateObject.animation.Play("downPOS-upPOS");
}
more ▼

answered May 23 '10 at 04:26 PM

ThumbStorm gravatar image

ThumbStorm
420 27 33 42

Thank you, I managed to figure it out from this, :)

May 24 '10 at 04:26 AM FraktuRe

There's another way I just heard about. I can't remember the name so just google tween and unity, it's a free add-on and works like tweening in flash.

May 24 '10 at 07:41 PM ThumbStorm
(comments are locked)
10|3000 characters needed characters left
function OnCollisionEnter (col : Collision) {

yield WaitForSeconds(3);
transform.position -= Vector3.up;
yield WaitForSeconds(3);
transform.position += Vector3.up;

}

moves 1m down after 3 seconds, then 1m up after 3 more seconds (added as an alternative to the good animation answer above)

more ▼

answered May 23 '10 at 06:33 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

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

I don't know Javascript, so this is going to be in C#, but I think it will be easy enough to understand that you can convert it.

You need to start a coroutine when it gets hit to do everything you want.

void OnCollisionEnter(Collision other)
{
     StartCoroutine(CollisionEvents());
}

IEnumerator CollisionEvents()
{
     // Send score here
     yield return new WaitForSeconds(1);
     // Hide here
     yield return new WaitForSeconds(5);
     // Show here
}
more ▼

answered May 23 '10 at 05:44 AM

qJake gravatar image

qJake
11.6k 43 78 161

All that appears to be is a c# version of what I already have?

May 23 '10 at 02:01 PM FraktuRe

Oh... apparently you can just yield whenever the hell you want to with Javascript... something else I should add to the list of things I hate about that language. :/

May 23 '10 at 09:54 PM qJake
(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:

x2506
x1374
x16

asked: May 23 '10 at 03:28 AM

Seen: 996 times

Last Updated: May 23 '10 at 03:28 AM