Enemy walking off platform in Unity2D
In my game the enemy is suposed to be walking back and forth on top of a platform. I use raycast to notice the edge, but it only works once. The enemy will start on the left side, before moving to the right side and turning. The problem is that the enemy does not turn when it hits the left side. The ray is cast from an empty object right in front of the enemy. The object does seem to follow the enemy for the entire duration, but still it wont work. Anyone got a clue of what the problem might be?
Script for the rays:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Patrol : MonoBehaviour
{
public float speed;
public float distance;
private bool movingRight = true;
public Transform groundDetection;
void FixedUpdate()
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
if (groundInfo.collider == false)
{
if(movingRight == true)
{
transform.eulerAngles = new Vector2(0, -180);
movingRight = false;
}
else
{
transform.eulerAngles = new Vector2(0, 180);
movingRight = true;
}
}
}
}
Your answer
Follow this Question
Related Questions
Animations not playing correctly for FPS enemy AI 0 Answers
Animations not playing correctly for FPS enemy AI 0 Answers
More advanced Top down enemy AI 0 Answers
AI Raycasting 0 Answers
AI Raycasting Question (Almost there!!!) 0 Answers