- Home /
Problem With RayCast
So I'm trying to have a raycast fire at the nearest target and return the object's that was hit name. I added Debug.Draw to see if it was the raycast and as far as I see the raycast doesn't do anything, not sure what I did wrong; please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrappleHook : MonoBehaviour
{
public RaycastHit HitInfo;
private GameObject GetClosestTarget(string targetTag)
{
GameObject closestTarget = null;
float closestDistance = 3.0F;
foreach (GameObject target in GameObject.FindGameObjectsWithTag(targetTag))
{
float dist = Vector3.Distance(transform.position, target.transform.position);
if (dist < closestDistance)
{
closestTarget = target;
closestDistance = dist;
}
}
return closestTarget;
}
private void Update()
{
GameObject ClosestTarget = GetClosestTarget("Enemy");
transform.LookAt(ClosestTarget.transform);
if(Physics.Raycast(transform.position, transform.forward, out HitInfo))
{
print(HitInfo.collider.gameObject.name);
Debug.DrawLine(Vector2.zero, HitInfo.point); //Doesn't Draw Anything
}
}
}
Answer by Vega4Life · Dec 18, 2018 at 12:29 AM
If you want to see where the raycast is visually, debug outside the raycast
Debug.DrawLine(transform.position, transform.forward, Color.red);
if(Physics.Raycast(transform.position, transform.forward, out HitInfo))
{
print(HitInfo.collider.gameObject.name);
}
This will draw a red line from your character to 1meter forward - which is the exact length and direction of your raycast.
Thx you, but it still doesn't do anything. I moved Debug.DrawLine like you said, but it still has no effect.
It doesn't draw in game view, only the scene view.
You should also probably make it longer because the transform gizmo arrows are 1meter. So do this
Debug.DrawLine(transform.position, transform.forward * 5f, Color.red);
THX U, IT WOR$$anonymous$$S NOW. I don't know how, but I deleted all my code and rewrote it and it worked now (with that change of course).
Answer by Cornelis-de-Jager · Dec 18, 2018 at 09:09 PM
I think it is drawing but just too short to see because you added no distance. Maybe.
Try the code below:
private void Update()
{
GameObject ClosestTarget = GetClosestTarget("Enemy");
transform.LookAt(ClosestTarget.transform);
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 2, Color.white);
if(Physics.Raycast(transform.position, transform.forward, out HitInfo))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.Red);
}
}
}
Answer by Golden_Gamer · Dec 18, 2018 at 11:50 PM
Oh it's only in the scene view, size isn't a problem; it went as far as I could see, but now I know the raycast is what is wrong with the code, it wont follow the closest target like I want. Thank You For The Help.
Your answer
Follow this Question
Related Questions
Raycast not working on build 0 Answers
How to find what type of tile player is on in 2D game 1 Answer
Can't raycast on Character Controller? 1 Answer
detection raycast too late 2 Answers
RayCast Nearest Character 0 Answers