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?

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).