Hi, basically i want to put AI into my game (just movement to follow the main character until they crash into random obstacles and die, or follow a path).
Here’s a quick look at what my game looks like Screenshot by Lightshot
I use no gravity for my main character/ship, and use forward Force and sideways Force to move it >.> Is that easy to use for AI?
Also i have a theory that i can create an AI path route by using the “rail track” thing, where u make the camera follow a specific route, but i use AI instead of a camera if that makes sense? (never used it though, nor know the correct name lol).
Basically does anyone know if i’m onto something? Or can you point me into the right way for AI in spaceships (just moves forwards, doesn’t fly like an actual jet/spaceship)
Here’s my simple movement script to make it more clear what type of movement i have:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float yaw = 0.0f;
public float pitch = 0.0f;
public float roll = 0.0f;
public float rollSpeed = 5.0f;
public float rollAmount = 0.0f;
public float MaxPitch = 90.0f;
public float MinPitch = -30.0f;
public float MaxRoll = 30.0f;
public float RotSpeed = 25.0f;
public float acceleration = 1.0f;
public float maxSpeed = 60.0f;
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
private float curSpeed = 0.0f;
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
pitch = Mathf.Clamp(pitch, MinPitch, MaxPitch);
roll = Mathf.Clamp(roll, -MaxRoll, MaxRoll);
transform.rotation = Quaternion.Euler(pitch, yaw, roll);
rollAmount *= 0.9f;
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
rollAmount -= rollSpeed;
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
rollAmount += rollSpeed;
}
if (Input.GetKey("w"))
{
rb.AddForce(0, sidewaysForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, -sidewaysForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (Input.GetKey(KeyCode.RightArrow))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
rollAmount -= rollSpeed;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
rollAmount += rollSpeed;
}
if (Input.GetKey(KeyCode.UpArrow))
{
rb.AddForce(0, sidewaysForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (Input.GetKey(KeyCode.DownArrow))
{
rb.AddForce(0, -sidewaysForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
transform.Rotate(0, 0, rollAmount * Time.deltaTime);
transform.Translate(Vector3.forward * curSpeed);
curSpeed += acceleration;
if (curSpeed > maxSpeed)
curSpeed = maxSpeed;
}
}