- Home /
Enemy rotates when hits wall
hi so i have a 2d game where an enemy goes right>left and left>right but i want the enemy to rotate towards the direction he is moving so move right > left >wall> rotate 180 > right >left here is the code. thank you
public float walkSpeed = 2.0f;
public float wallLeft = 0.0f;
public float wallRight = 5.0f;
float walkingDirection = 1.0f;
Vector2 walkAmount;
// Update is called once per frame
void Update () {
walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
if (walkingDirection > 0.0f && transform.position.x >= wallRight)
walkingDirection = -1.0f;
else if (walkingDirection < 0.0f && transform.position.x <= wallLeft)
walkingDirection = 1.0f;
transform.Translate(walkAmount);
}
}
Comment
This questions makes little sense without context, and is simply a "fix my code" question, which of course, impossible without context.
Your answer
Follow this Question
Related Questions
Enemy Pirate Ship AI 1 Answer
Make my camera turn smoothly 2 Answers
my enemy isn't looking at the player in 2d 1 Answer
Camera rotation around player while following. 6 Answers
Rotation of my enemy How?? 2 Answers