I am trying to make a space trek game and have got to the point that I need to make a ship AI so I looked around and tried this code for following the player which will at some point shoot at me.
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour
{
public Transform target; //the enemy's target
float moveSpeed = 3; //move speed
float rotationSpeed = 3; //speed of turning
public Transform myTransform; //current transform data of this enemy
void Awake()
{
myTransform = transform;
}
void Start()
{
target = GameObject.FindWithTag("[Ally] Excelsior").transform; //target the player
}
void Update()
{
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
Problem is that it follows my ship with the front up in the air looking strange as it comes at me lol What’s happening? I don’t really have any idea what a Quaternion is… any other advice since I don’t want it to run into me but rather come at me and orbit me till I run and it follows. Does that make any sense? Kind of like in STO…
Your script is using Slerp incorrectly. You keep moving each frame a fraction of what remains of the rotation. That means you’ll never reach. Think that you’re moving halfway towards your destination every frame. Will you ever reach it? No. You always move half of what remains, so there will always remain the other half…
Try this instead:
void Update()
{
// rotate towards player
Vector3 toTarget = (target.position - myTransform.position).normalized;
myTransform.forward = Vector3.RotateTowards(myTransform.forward, toTarget, float rotationSpeed * Time.deltaTime, 0);
// move towards the player
myTransform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
Quaternion.LookRotation() takes two parameters which describe the “forward” and “up” vectors of the desired rotation.
In the following line, you’re setting the “forward” direction equal to the vector between the target and the the current position, which seems ok, but you’re setting the “up” direction to be equal to rotationSpeed*Time.deltaTime, which seems a little funky…
Well, usually AI is made using beaviour trees (FSMs), in wich you must define several conditions for a certain action to happen. And that usually includes a lot of pen and paper.
I do happen to be doing a Star Trek based game (named Star Trek: Broken Mirror), and I remember we had to do a lot of stuff before even working on things like AI, so we could gain eough experience to tackle that problem. Maybe you should also do the same… Tackle things like ship movement, weapon targeting, create a solid movement system with well defined interfaces (public methods, that is), and some other things like a friend-or-foe system, and then you might be reaady to handle the AI.
You should also read something about the theme… Unity Gems has this fine series of tutorials that might hep you with that: