My enemy cube moves through walls
Hi, I am a beginner at using unity and after doing the roll-a-ball tutorial I decided I am making a platformer project using the movement concept from the tutorial. I have gotten to the point where I want to add obstacles and "enemies" that need to be avoided. Right now I am basically trying to get the enemy to pingpong between x values so that it moves back and forth and therefore makes the player have to avoid it to progress.
Well, right now I am using this code for my script.
public float speed = 3;
public float startingX;
public float targetX;
public float direction;
void Update () {
transform.position = new Vector3(direction * Mathf.PingPong(Time.time * speed, targetX * 2) + startingX * direction, transform.position.y, transform.position.z);
}
}
And the problem is the enemy is not ping ponging and ends up moving through the wall boundaries and going on in the x positive direction to infinity and beyond. Can anyone help me? It'd be much appreciated. As well as any explanations to help me understand what is happening would be lovely.
Your answer
Follow this Question
Related Questions
how to rotate a gameobject between two angles front and back direction? 0 Answers
Pausing a Mathf.PingPong at the ends 0 Answers
what exactly does Time.time do in Mathf.PingPong? 3 Answers
how to make mathf.pingpong not from 0 3 Answers
Separate Numbers ? 1 Answer