- Home /
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!
Answer by ThumbStorm · May 23, 2010 at 04:26 PM
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"); }
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.
Answer by qJake · May 23, 2010 at 05:44 AM
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 }
All that appears to be is a c# version of what I already have?
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. :/
Answer by Mike 3 · May 23, 2010 at 06:33 PM
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)
Your answer
Follow this Question
Related Questions
How to stop a character from moving out of player's control? 0 Answers
Boat collides with track(plane) below it while moving forward 0 Answers
Simple moving ball script 1 Answer
Adding collision detection to movement script... 1 Answer
How to move multiple dynamic gameobjects with one moving platform which is controlled by the player 0 Answers