Hi! I’m making the AI for a race game in a pip and i need to know the way of doing something like:
- Raycast forward
- if tag enemy
- if distance < 20 START COROUTINE Avoid()
- in Avoid RANDOM turn direction by change the turnSpeed var SMOOTHLY
The systm works with the var turnSpeed. As it increments the ship transform.position vector.
// DRIVE FORWARD
Vector3 forward = transform.TransformDirection (Vector3.forward);
transform.position = transform.position + forward * speed * Time.deltaTime;
// TURN AROUND
Vector3 turn = transform.TransformDirection (Vector3.right);
transform.position = transform.position + turn * turnSpeed * Time.deltaTime;
The othe part of the script is:
// AVOID OTHER SHIPS
Vector3 front = transform.TransformDirection (Vector3.forward);
RaycastHit frontHit;
if (Physics.Raycast (transform.position, front, out frontHit)) {
if (frontHit.collider.tag == "Ship") {
if (frontHit.distance < 20.0F) {
StartCoroutine (Avoid ());
}
}
}
ANd the coroutines:
IEnumerator Avoid () {
int randomNum = Random.Range (0, 2);
if (randomNum == 0) {
yield return StartCoroutine (TurnLeft ());
yield return StartCoroutine (TurnRight ());
}
if (randomNum == 1) {
yield return StartCoroutine (TurnRight ());
yield return StartCoroutine (TurnLeft ());
}
}
IEnumerator TurnLeft () {
turnSpeed -= turnAccel * speed * Time.deltaTime * 0.2F;
yield return new WaitForSeconds (0.5F);
turnSpeed = 0.0F;
}
IEnumerator TurnRight () {
turnSpeed += turnAccel * speed * Time.deltaTime * 0.2F;
yield return new WaitForSeconds (0.5F);
turnSpeed = 0.0F;
}
If it can’t be done with coroutines, I would like to know other methods…
Thanks