- Home /
Debug.log not working with raycast
Hi guys, its my first time here! I want an enemy that uses raycasts to detect me, the player. Im setting up the raycast and a test debug log to see if it works, but it does not. On my enemy I put a sphere and put a collider with is trigger on it. The problem is that even tho it enters the trigger, it does not show the debug message.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
private GameObject playerTarget;
private SphereCollider col;
void Awake ()
{
playerTarget = GameObject.FindGameObjectWithTag ("Player");
col = GetComponent<SphereCollider>();
}
void OnTriggerEnter (Collider other)
{
if(other.gameObject == playerTarget)
{
Vector3 direction = playerTarget.transform.position - transform.position;
{
RaycastHit hit;
if(Physics.Raycast(transform.position, direction.normalized, out hit, col.radius))
{
if(hit.collider.gameObject == playerTarget)
{
Debug.Log ("player in sight");
//Stuff happens that makes the enemy chase the player
}
}
}
}
}
}
I hope somebody can help me with this one because I'm completely stuck now...
I doubt that is debug.log that does not work with raycast. But insert a debug log before line 25. But you qould be hitting something unexpected. Perhaps the collider isa child of the PlayerTarget
Debug.Log ("Hitted"+hit.transform.name);
I placed the Debug.Log ("player in sight"); on multiple places in the code, but I'm not getting a response, even in the awake. I am able to see the green orb around my enemy cube, the collision orb. I enter the orb with the player... and nothing happens. Any ideas?
I have the same problem, when my raycast has detected each game object, Debug.Log return the last one, even if i put a clear line to erase it , it doesn't refresh, but the essential is that raycast run...good luck
If you don't get a log even if you put it into Awake, the script is probably not attached to the enemy in the scene.
Are you sure that playerTarget is not null?
Another question would be how you move the player. If by translation, then that's the problem.
BTW. you can remove the braces on line 20 and 31, they're not part of any condition.
Last but not least: why are you raycasting what you already triggered, doesn't make sense.
Answer by Ericool · Mar 24, 2015 at 04:38 PM
well using OnTriggerEnter and RayCast are two differents techniques first off . If we enter in OnTriggerEnter that means that isTrigger is enable , check that first . Second , OnTriggerEnter works once your objects collide until you went out to call OnTriggerExit. Third , RayCast might not working it is calculating a distance , but if your objects are too close , dunno..
When Update()
{
if(activateRayCasting)
{
//do your raycast stuff
}
}
When OntriggerExit(...)
{
activateRayCasting = true;
}
When OntriggerEnter(..)
{
activateRayCasting = false;
if(col.gameObject.tag != "terrain")
Debug.Log(col.gameObject.name);
}
Your answer
Follow this Question
Related Questions
Follow Nearest Target 1 Answer
Boolean being overwritten 1 Answer
Raycast not reporting what it hit 1 Answer
can foe hear us? 1 Answer
How to see if a transform is visible without a camera 1 Answer