- Home /
Question by
Zero23ku · Mar 05, 2017 at 03:31 PM ·
movementunity 2dmovement script
Enemy shakes on Y-axis when is moving straight on the X-axis
First at all, sorry for my bad english.
Basically I have an enemy who moves toward player on Y-Axis and X-Axis.
When it moves in both, Y-Axis and X-Axis it works well, also, when it moves only in the Y-Axis, but when the enemy moves only in the X-axis it shakes on the Y-Axis a little bit but I don't know why.
Here's the code of the movement method.
void Movement() {
if (player)
playerPosition = playerTransform.position;
selfPosition = selfTransform.position;
//Distance between player and enemy//
distance = playerPosition - selfPosition;
if (distance.x <= 0.0f) {
selfBody.velocity = new Vector2(-enemyInformation.speed,selfBody.velocity.y);
if (!isLookingLeft) {
Flip();
}
}
else {
selfBody.velocity = new Vector2(enemyInformation.speed, selfBody.velocity.y);
if (isLookingLeft) {
Flip();
}
}
if (distance.y <= -0.5f) {
selfBody.velocity = new Vector2(selfBody.velocity.x, -enemyInformation.speed);
}
else {
selfBody.velocity = new Vector2(selfBody.velocity.x, enemyInformation.speed);
}
}
Comment