- Home /
Raycast is not working
Hello i have a script that controlls my enemy, i have so it follows me (futher down the script). I also have an raycast that checks if the Player is close to it, but i cant position the raycast right and it always changes please help! (Script is atatched to the enemy and the enemy rotates towards the player/target)
Code:
function Update ()
{
//Makes the enemy move
if(Input.GetKeyDown(KeyCode.Y))
{
move = true;
}
//Stop in front of player
var hit : RaycastHit;
var ray : Ray = new Ray(transform.position, Vector2.right);
if (Physics.Raycast (ray, hit, raylength))
{
Debug.DrawLine (transform.position, hit.point, Color.red);
if (hit.collider.gameObject.CompareTag("Player"))
{
move = false;
}
}
}
Have you tried doing a draw line on your ray to see if the line his your player? Also, if you're just doing a distance check then a raycast is probably an unnecessarily expensive way to do it.
I have a debug.log so I see my ray and it is positioned wrong and is rotated wrong how so I fix that?
Answer by karl_jones · Jun 01, 2015 at 11:58 AM
Instead of Vector2.right try transform.right. This will take into account the orientation of your object so it always does right from the object and not the world.
Thank you but how do i position it a little highter up? becouse its in the right rotation but its to far down so it doesn't collide with my player
Your answer
Follow this Question
Related Questions
Unity Character Controller messing up. 0 Answers
Follow mouse until mouseclick 0 Answers
hit.gameObject.AddComponent not working 1 Answer