I am having trouble rotating my raycast (LineOfSite) on my enemy player. So when the enemy is patrolling the area. The player is able to steps on the ray cast, behind it because it set it up to point out left, and make them stop moving. Is it possible to flip the raycast like you flip the player? I tried transforming.Left and localscale and i end up getting errors.
NOTE: I am trying to keep the line in front of the enemy’s face
Code: hatebin
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Patrol : MonoBehaviour
{
public float speed;
public float raylength, LosDis;
private Transform TPlayer;
private bool movingLeft = true;
public Transform detection;
// Start is called before the first frame update
void Start()
{
Physics2D.queriesStartInColliders = false;
}
// Update is called once per frame
void Update()
{
patrolling();
LineofSight();
}
public void patrolling()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
RaycastHit2D ground = Physics2D.Raycast(detection.position, Vector2.down, raylength);
if (ground.collider == false)
{
if (movingLeft == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
movingLeft = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
movingLeft = true;
}
}
}
public void LineofSight()
{
RaycastHit2D enemyspoted = Physics2D.Raycast(transform.position, transform.forward, LosDis);
if (enemyspoted.collider != null) {
print("Player hit!");
Debug.DrawLine(transform.position, enemyspoted.point, Color.red);
TPlayer = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
transform.position = Vector2.MoveTowards(transform.position, TPlayer.position, speed * Time.deltaTime);
}else {
Debug.DrawLine(transform.position, transform.position + Vector3.left * LosDis, Color.green);
}
}
}//patrol