Else If running even though conditions not met
Okay, I've been staring at this thing for a long time with no success, so any help will be appreciated. In the code below, I have an enemy character that patrols a spot until the player comes within distance of the raycast. While the player is hit by the raycast, the enemy should stop patrolling and start shooting at the player. The problem I'm running into is that the enemy unit continues patrolling between shots. He will stop, shoot, walk again until the timer runs out, stop, shoot, etc. I feel like something about the timer I've set up is messing with the raycast since the enemy shouldn't patrol while the player is within range, but I can't for the life of me figure out how to fix it. I'm a total noob, so simple answers will be appreciated.
void Update () {
playerPos = GameObject.FindGameObjectWithTag ("Player").transform.position;
direction = (playerPos - (new Vector2 (transform.position.x, transform.position.y))).normalized;
hit = Physics2D.Raycast (transform.position, direction, bulletDistance);
tempBulletsTimer -= Time.deltaTime;
Debug.Log (hit);
if (hit.collider != null && hit.collider.tag == Tag.Player && tempBulletsTimer <= 0) {
Instantiate (bullet, transform.position, transform.rotation);
Debug.DrawLine (transform.position, playerPos, Color.red, .01f);
tempBulletsTimer = 1;
} else if (hit.collider == null || hit.collider.tag != Tag.Player) {
transform.Translate (Patrol ());
}
}
Answer by Crawdad · Feb 17, 2017 at 08:15 PM
I managed to figure out a better way to approach this one that solved my problem. Thanks for the input.
Answer by IgorAherne · Feb 16, 2017 at 01:46 AM
Check this:
Transform playerTransform;
void Start() {
//start runs once game object (to which this script is attached) is bourn.
//Runs only once, so use this expensive "Find" function once, and not every frame
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update() {
//you had something weird going on with Vector2 here:
direction = (playerTransform.position - transform.position).normalized;
hit = Physics2D.Raycast(transform.position, direction, bulletDistance);
tempBulletsTimer -= Time.deltaTime;
if (hit.collider == null || hit.collider.tag != Tag.Player) {
transform.Translate(Patrol());
return;
}
//otherwise, we still didn't return. Conditions must have been met!
//we are now sure that "hit" is NOT null and the player has correct tag.
if (tempBulletsTimer <= 0) {
Instantiate(bullet, transform.position, transform.rotation);
Debug.DrawLine(transform.position, playerPos, Color.red, .01f);
tempBulletsTimer = 1;
}
}//end Update()
}
Thanks for suggestions, Igor. I tried this setup and, although better thought out than what I had, the result was ultimately the same. The only modifications I had to make to your script was to keep the playerTransform variable in the update section so that the enemy shot towards his current location, and not his location at the beginning of the scene. It still seems like the raycast returns null during a portion of the function even while the player is well within the raycast distance. If you have any other ideas, please let me know. Thanks again.