- Home /
How to rotate the raycast on a patrolling enemy? (In front of the enemy at all times)
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: https://hatebin.com/scrbhqbkrc
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
First question is, why do you draw your line with different parameters than your raycast? Also your code looks good, I don't really see the problem, maybe double check if you are using the correct axis?
The easiest workaround would be to use directions in world space depending on your movingLeft bool, but that's ugly, your current solution is a lot better (if it worked)
I have two different raycas. 1patrol and 2. the Line of Sight for the enemy. The 2nd one is what i am having trouble with.
your code seems fine just change Vector3.left to transform.forward in the last drawline method, can you try debug.log transform.forward and make sure its actually changing? you are NEVER changing LosDis value to negative righ?
When I use tranform.forward my debug line disappears and it also doesn't activate the chase when the player collides into the line of sight. Also I am not changing my LosDis value
that makes no sense, whats printing if you debug.log transform.forward?
Answer by Luvarv · Apr 10, 2019 at 07:50 AM
I am guessing this is a side scroller. You might be able to use something like:
new Vector 2 (dir, 0)
instead of:
transform.forward
and set "dir" to -1 when looking left and to 1 when looking right.
it makes no sense his error, it cant be a side collider since when patrolling when changing the direction he is changing the transform.rotation by 180*, so transform.forward should be affected to, or i am missing something?
I'm sorry it is a 2D platform game. I should have specified that in the beginning.
That gave me an Error
public void LineofSight()
{
RaycastHit2D enemyspoted = Physics2D.Raycast(transform.position, new Vector2(Dir, 0), LosDis);
if (enemyspoted.collider != null)
{
Debug.DrawLine(transform.position, enemyspoted.point, Color.red);
if (enemyspoted.collider.CompareTag("Player")) {
TPlayer = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
transform.position = Vector2.$$anonymous$$oveTowards(transform.position, TPlayer.position, speed * Time.deltaTime);
}
}
else
{
Debug.DrawLine(transform.position, transform.position + Vector3.left * LosDis, Color.green);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612195025im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy stops patrolling when player comes close 2 Answers
Y rotation of object based on center of a sphere? 0 Answers
Raycast gun 2 Answers
Infinite Raycast? 1 Answer