Problem animating in Unity/Orthello 2D. Can't move gameObject
Posted
by
Nelson Gregório
on Game Development
See other posts from Game Development
or by Nelson Gregório
Published on 2012-11-05T03:28:33Z
Indexed on
2012/11/05
5:14 UTC
Read the original article
Hit count: 273
I have a enemy npc that moves left and right in a corridor. It's animated with 2 sprites using Orthello 2D Framework.
If I untick the animation's play on start and looping, the npc moves correctly.
If I turn it on, the npc tries to move but is pulled back to his starting position again and again because of the animation loop.
If I turn looping off during runtime, the npc moves correctly again.
What did I do wrong?
Here's the npc code if needed.
using UnityEngine;
using System.Collections;
public class Enemies : MonoBehaviour
{
private Vector2 movement;
public float moveSpeed = 200;
public bool started = true;
public bool blockedRight = false;
public bool blockedLeft = false;
public GameObject BorderL;
public GameObject BorderR;
void Update ()
{
if (gameObject.transform.position.x < BorderL.transform.position.x)
{
started = false;
blockedRight = false;
blockedLeft = true;
}
if (gameObject.transform.position.x > BorderR.transform.position.x)
{
started = false;
blockedLeft = false;
blockedRight = true;
}
if(started)
{
movement = new Vector2(1, 0f);
movement *= Time.deltaTime*moveSpeed;
gameObject.transform.Translate(movement.x,movement.y, 0f);
}
if(!blockedRight && !started && blockedLeft)
{
movement = new Vector2(1, 0f);
movement *= Time.deltaTime*moveSpeed;
gameObject.transform.Translate(movement.x,movement.y, 0f);
}
if(!blockedLeft && !started && blockedRight)
{
movement = new Vector2(-1, 0f);
movement *= Time.deltaTime*moveSpeed;
gameObject.transform.Translate(movement.x,movement.y, 0f);
}
}
}
© Game Development or respective owner