I’m having an error in my raycast code. It seems that i cannot use else in raycast?
Or is there something else wrong in this code.
public Transform player;
public bool canSee = false;
void OnWillRenderObject() {
RaycastHit hit;
if (Physics.Raycast(player.position, (transform.position - player.position).normalized, out hit)) {
if (hit.collider.gameObject == gameObject) {
canSee = true;
}
else{
canSee = false;
if(canSee == false){
Debug.Log ("Player not in sight :(");
}else{
Debug.Log ("Player in sight attack >:D");
}
}
}
}
Yes it is. I'm wondering that when i look at enemy the canSee goes true. But when i turn 180 degrees it remains true even though i'm not seeing the enemy. But when i look at the enemy direction through a wall it sets it false.
– Michael.HThat's the problem: when the camera looks in another direction, OnWillRenderObject may stop being called before registering that the object isn't visible anymore.
– aldonalettoI put the canSee = false inside a void Update() and its now working. Thanks a lot! Props on you sir!
– Michael.HPlease submit this as a separate question.
– meat5000