I am currently working on a prototype of a flight simulator. However, I have run into a problem while trying to make sure enemies do not crash into walls. Here’s my code so far:
using UnityEngine;
using System.Collections;
public class Enemy1Script : MonoBehaviour {
float hp = 1f;
public float speed;
public float turnSpeed;
public float gunSpeed;
public float evasion;
public GameObject explosion;
public GameObject bullet;
public Transform gun;
public Transform rayTransform;
float timer;
void FixedUpdate () {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit hitObject;
bool objectAhead = Physics.Raycast(rayTransform.position, fwd, out hitObject, speed * evasion);
int x; //Spare variable
Debug.DrawRay(rayTransform.position, fwd * speed * evasion);
if (objectAhead) {
Debug.Log("Object ahead");
if (hitObject.collider.gameObject.tag == "Default") {
Debug.Log("Enemy ahead");
Quaternion y = rayTransform.rotation;
transform.Rotate(0,0, turnSpeed * Time.deltaTime);
}
if (hitObject.collider.gameObject.tag == "Wall") {
Debug.Log("Obstacle ahead");
x = evasiveManeuvers();
if (x == 1) transform.Rotate(-turnSpeed * Time.deltaTime, 0, 0);
else if (x == 2) transform.Rotate(-turnSpeed * Time.deltaTime, 0, turnSpeed * Time.deltaTime);
else if (x == 3) transform.Rotate(-turnSpeed * Time.deltaTime, 0, -turnSpeed * Time.deltaTime);
else if (x == 4) transform.Rotate(turnSpeed * Time.deltaTime, 0, 0);
else Debug.Log("Evasive measures returned 0");
}
}
transform.Translate(0, 0, speed * Time.deltaTime);
}
void fire() {
if (timer >= gunSpeed && gunSpeed > 0f) {
Instantiate(bullet, gun.position, transform.rotation);
timer = -1;
}
}
int evasiveManeuvers(int x = 1, float lastRayLength = 0) {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
int a;
Quaternion rotation = transform.rotation;
RaycastHit rayHit;
bool objectAhead = Physics.Raycast(rayTransform.position, fwd, out rayHit, speed * evasion);
if (x > 200) return 0;
if (Physics.Raycast(rayTransform.position, fwd, speed * .5f)) return 0;
lastRayLength = rayHit.distance;
transform.Rotate(-turnSpeed * Time.deltaTime, 0, 0);
if (objectAhead) {
a = evasiveManeuvers(x++, rayHit.distance);
if (a != 0) {
transform.rotation = rotation;
return 1;
}
} else {
Debug.Log("Evasive maneuvers was called " + x + "times and returned 1");
return 1;
}
transform.rotation = rotation;
transform.Rotate(-turnSpeed * Time.deltaTime, 0, turnSpeed * Time.deltaTime);
if (objectAhead) {
a = evasiveManeuvers(x++, rayHit.distance);
if (a != 0) {
transform.rotation = rotation;
return 2;
}
} else {
Debug.Log("Evasive maneuvers was called " + x + "times and returned 2");
return 2;
}
transform.rotation = rotation;
transform.Rotate(-turnSpeed * Time.deltaTime, 0, -turnSpeed * Time.deltaTime);
if (objectAhead) {
a = evasiveManeuvers(x++, rayHit.distance);
if (a != 0) {
transform.rotation = rotation;
return 3;
}
} else {
Debug.Log("Evasive maneuvers was called " + x + "times and returned 3");
return 3;
}
transform.rotation = rotation;
transform.Rotate(turnSpeed * Time.deltaTime, 0, 0);
if (objectAhead) {
a = evasiveManeuvers(x++, rayHit.distance);
if (a != 0) {
transform.rotation = rotation;
return 4;
}
} else {
Debug.Log("Evasive maneuvers was called " + x + "times and returned 4");
return 4;
}
transform.rotation = rotation;
return 0;
}
}
It seems to be working well enough (although I admit I haven’t tested it that much), but the main problem is that the game’s framerate slows to a crawl whenever it begins turning. This will especially become a serious problem when multiple enemies are on the screen. It has even crashed Unity on two occasions. Does anybody have any ideas about how to make this more efficient?