Enemy doesn't flip properly with linecast
Hello all! I am having an issue with some Goomba AI I'm trying to make with linecasts. The issue is that the enemy doesn't flip properly when it makes contact with an obstacle, instead, it just flips back and forth continuing in the same direction.
My code is as follows.
Debug.DrawLine(sightStart.position, sightEnd.position, Color.green);
obstaclehit = Physics2D.Linecast(sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("Obstacle"));
transform.Translate (new Vector3(speed, 0, 0) * Time.deltaTime);
if(obstaclehit == true)
{
speed *= -1;
}
Any help pointing me in the right direction would be greatly appreciated!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612112031im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Boss ai help EoW 0 Answers
C# Script - Erro CS8025: Parsing error 1 Answer