Hey Guys,
When the player collides with the enemy, the enemy keeps rotating all around itself. How can I prevent that from happening?
The Enemy Script:
public class EnemyController : MonoBehaviour
{
public float speed;
public bool vertical;
public float changeTime = 3.0f;
public ParticleSystem smokeEffect;
Rigidbody2D rigidbody2d;
float timer;
int direction = 1;
bool broken = true;
Animator animator;
void Start()
{
rigidbody2d = GetComponent();
timer = changeTime;
animator = GetComponent();
}
void Update()
{
if (!broken)
{
return;
}
timer -= Time.deltaTime;
if (timer < 0)
{
direction = -direction;
timer = changeTime;
}
}
void FixedUpdate()
{
if (!broken)
{
return;
}
Vector2 position = GetComponent().position;
if (vertical)
{
position.y = position.y + Time.deltaTime * speed * direction;
animator.SetFloat(“Move X”, 0);
animator.SetFloat(“Move Y”, direction);
}
else
{
position.x = position.x + Time.deltaTime * speed * direction;
animator.SetFloat(“Move X”, direction);
animator.SetFloat(“Move Y”, 0);
}
GetComponent().MovePosition(position);
}
void OnCollisionEnter2D(Collision2D other)
{
RubyController player = other.gameObject.GetComponent();
if (player != null)
{
player.ChangeHealth(-1);
}
}
public void Fix()
{
broken = false;
GetComponent().simulated = false;
animator.SetTrigger(“Fixed”);
smokeEffect.Stop();
}
}
Thanks in Advance