- Home /
Question by
gotnull · Jul 03, 2015 at 01:55 PM ·
animationupdatestartcoroutine
Setting position after respawn.
I have a relatively easy question on how to position the Y-axis on a game object and how to wait for an animation to finish before my player is re-spawned to a checkpoint.
This is the code I have so far:
public void RespawnPlayer ()
{
body2d.gravityScale = 0f;
var pos = player.transform.position;
pos.y = currentCheckpoint.transform.position.y - 0.068f;
pos.x = currentCheckpoint.transform.position.x;
player.transform.position = pos;
player.warping = true;
player.warpStarted = false;
}
This is called from a coroutine in an Update()
:
void Update ()
{
if (!warping) {
if (collisionState.standing) {
ChangeAnimationState (0);
}
} else if (!warpStarted) {
StartCoroutine (Warp ());
warpStarted = true;
}
}
IEnumerator Warp ()
{
Debug.Log ("Should only execute once.");
ChangeAnimationState (5); // Ideally I'd like to get the total time of the animation and set the WaitForSeconds with that time.
yield return new WaitForSeconds (1);
body2d.gravityScale = 5f;
warping = false;
}
void ChangeAnimationState (int value)
{
animator.SetInteger ("AnimState", value);
}
The problem I'm facing is that I see for a split second that the the position of the sprite is in the correct position but then straight away changes to the position of the currentCheckpoint
in the RespawnPlayer()
method.
Is there a better way of achieving what I'm after?
Comment
Your answer
Follow this Question
Related Questions
Parent and child repositioning 0 Answers
Game broken after Unity 4.5.1 update 1 Answer
Handling Movement - Animations 1 Answer