Hello, this is my first question here, I am a beginner with Unity but I have used C# before so I do know the scripting basics with this language.
Problem:
I’m having a bit of trouble nesting an “else” statement inside another else. I thought this was possible but for some reason only my “if” and “else if” statements are being fired inside my main “else” statement. I’m creating a simple ai system which uses raycasts, public booleans and debug logs in combination to give specific information about what the enemy gameobject is doing at any given moment. I’ll explain the basic script further below, but first here it is:
using UnityEngine;
using System.Collections;
public class SimpleAISystem : MonoBehaviour {
public Transform target; //Target we're moving towards
public float nudgeSpeed = 1;
public float speed = 2;
public float rotateSpeed = 10;
public float rotateAngle = 45;
private int direction = 1;
public float forwardRaycastLength = 5;
public float leftRaycastLength = 5;
public float rightRaycastLength = 5;
public bool raycastForwardHit = false;
public bool raycastLeftHit = false;
public bool raycastRightHit = false;
RaycastHit hit = new RaycastHit();
// Update is called once per frame
void Update () {
Debug.Log(direction);
//Forward Raycast
if(!Physics.Raycast(transform.position, transform.forward, out hit, forwardRaycastLength)){
Debug.DrawLine(transform.position, transform.forward * forwardRaycastLength, Color.blue);
//transform.Rotate(Vector3.up, rotateAngle * rotateSpeed * Time.smoothDeltaTime * direction);
transform.position += transform.forward * speed * Time.deltaTime;
raycastForwardHit = false;
Debug.Log("Front Raycast");
}
else{
Debug.Log("Outer Else");
//Forward Left Raycast
if(Physics.Raycast(transform.position, -transform.right, out hit, leftRaycastLength)){
Debug.DrawLine(transform.position, -transform.right * leftRaycastLength, Color.yellow);
if (hit.transform != transform && !hit.collider.gameObject.CompareTag("Ground")) {
direction = 1;
raycastLeftHit = true;
Debug.Log("Left Raycast");
}
}
//Forward Right Raycast
else if(Physics.Raycast(transform.position, transform.right, out hit, rightRaycastLength)){
Debug.DrawLine(transform.position, transform.right * rightRaycastLength, Color.red);
if (hit.transform != transform && !hit.collider.gameObject.CompareTag("Ground")) {
direction = -1;
raycastRightHit = true;
Debug.Log("Right Raycast");
}
}
//Otherwise set our left & right booleans back to false
else {
raycastLeftHit = false;
raycastRightHit = false;
Debug.Log("Nested Else");
}
//transform.position += transform.forward * speed * Time.deltaTime;
transform.Rotate(Vector3.up, rotateAngle * rotateSpeed * Time.smoothDeltaTime * direction);
raycastForwardHit = true;
}
//If our Forward Left & Forward Right raycast booleans are set to true, (the Enemy will not
//know which way to turn) nudge Enemy forward
if(raycastLeftHit && raycastRightHit){
transform.position += transform.forward * nudgeSpeed * Time.smoothDeltaTime;
Debug.Log("Both");
}
//If none of our raycasts are hitting, look towards our target
if(!raycastForwardHit && !raycastLeftHit && !raycastRightHit){
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotateSpeed * Time.deltaTime);
Debug.Log("Look At target");
}
}
}
The else statement in question is just past halfway down below the “//Forward Right Raycast” with the Debug.Log(“Else”) in it. In short I am using a separate boolean(raycastForwardHit, raycastLeftHit & raycastRightHit) for each raycast which is set to true on hit. This allows me to check for when only an individual raycast hits and when several hit without needing to re-cast the same ray multiple times.
Problem (in detail):
The trouble then is that the nested else statement is supposed to be setting the booleans raycastLeftHit and raycastRightHit back to false when neither the Left Raycast or the Right Raycast are hitting anything. But this is never fired, not even the debug log and I can’t figure out why. I realise I could re-cast a !Physics.Raycast for each of them, but this is less efficient, especially since this script will be used by numerous gameobjects. How can I turn off the specific boolean when one of these raycasts is not hitting without re-casting?