- Home /
Raycast stops when false, even in update()
Hi there,
I've got a raycast to check whether a target can be fired at (as in, if he;s not behind a wall, etc)
here's the code-
Vector3 fwd = (target.transform.position - transform.position).normalized;
RaycastHit hit;
Debug.DrawRay(transform.position, fwd * 10, Color.blue);
if (Physics.Raycast(transform.position, fwd, out hit, 20)) {
Debug.Log(hit.collider.gameObject.name);
if(hit.collider.gameObject.tag == "enemy") {
controlthing.GetComponent<lightning>().shouldgo = true;
}
else if(hit.collider.gameObject.tag != "enemy") {
controlthing.GetComponent<lightning>().shouldgo = false;
}
}
Can anyone see what i'm doing wrong here?
What is the problem? Does it never shoot, or sometimes shoot if you are just behind a wall, etc... .
What does "stops when false" mean? Do you mean that it stops doing raycasts?
Answer by Tseng · Apr 14, 2012 at 01:03 PM
You sure you used "enemy" as tag and not "Enemy"? It's case sensitive.
Second, consider using "CompareTag" instead of ".tag == xyz". CompareTag will also check if the tag is available and give a warning if you compare an tag which doesn't exist.
Third, the second "else if" is not necessary. If the tag equals to enemy it goes in the "if"-branch if not it goes into "else". No need for extra check (and processing).
Your answer
Follow this Question
Related Questions
Getting the Furthest Point on a Ray 1 Answer
Why wont my raycast compareTag properly? 2 Answers
A node in a childnode? 1 Answer
Automatic Node Connection - Help 1 Answer