Question by
hidonut123 · Sep 04, 2015 at 05:22 AM ·
c#unity 52djavascriptsword
How to make a sprite retract to its original position?
I'm currently trying to implement a Yoshi's tongue, where it "whips" to a coordinate, and return back to it's original position. So far, I have gotten the tongue to leave the mouth, and travel vertically, but what could I add to it so I could achieve:
A max translation, (cannot moveTo further than this y-coordinate)
After an interval of ~0.5 seconds, the sprite retracts to it's original coordinate
Pushing the key down will not do anything while the tongue is at the maxTranslation, or while it is in motion
So far I have this code to make the tongue go out:
#pragma strict
function Start () {
transform.position.z = -0.5;
}
function Update () {
if(Input.GetKeyDown ("space"))
{
moveTo(transform.position.y + 10, 50); // Feed the moveTo() function the X/Y positions you want to move it to, and the Speed you want to move at
}
transform.position.z = -0.5;
}
function moveTo(posY : float, speed : float)
{
while (transform.position.y != posY)
{
transform.position = Vector2.MoveTowards (transform.position, new Vector2(transform.position.x, posY), speed * Time.deltaTime);
yield;
}
}
Thanks, any help is appreciated.
Comment