- Home /
Problems with simple AI script
Hi there I'm planning to put AI into my game so after some research I got something similar to what I want but it's still very buggy. The AI seems to only want to face the target's ass, so when I do a 180 turn with my player the AI spins around the player at the same distance. It's hard to explain without running it yourself but here it is.
using UnityEngine;
using System.Collections;
public class AIScript : MonoBehaviour
{
public GameObject target;
public float moveSpeed = 20;
public float rotationSpeed =5;
private float distance;
private float finalmove;
private float finalrot;
void Start()
{
finalmove = Random.Range(1, moveSpeed);
finalrot = Random.Range(1, rotationSpeed);
target = GameObject.FindGameObjectWithTag("Player");
}
void Update ()
{
distance = Vector3.Distance(target.transform.position, transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(target.transform.position - transform.position), finalrot * Time.deltaTime);
if (distance > 1)
{
transform.position += transform.forward * finalmove * Time.deltaTime;
}
}
}
Any help would be appreciated, even if it involves writing a new script
Thanks in advance
Hmmm.. Just to clarify, do you want the AI to remain static but keep looking at the player, and move forward toward him?
Also, for the looking-at-the-ass problem, you might consider placing an empty object that is parented to your player, to deter$$anonymous$$e the position where the AI should look. You should then give it a custom tag.. $$anonymous$$aybe "PlayerLookPoint" or whatever, just not the same as the player, then look for that tag when starting the game.
I'm sorry If I misunderstood but if the AI is looking at the player and moving towards it doesn't that make it dynamic not static? :|
In any case I would like the AI to follow the player and look at the player.
I'll try that empty object thing when I get a chance to work on it :)
A side note, would you recommend creating an empty game object for most things like that? It sounds like a good way to avoid unnecessary errors.
Sorry, yes, what I meant to ask.. Is it suppose to move in relation to the player or independently?
Also, Is the AI parented to the player? If the AI is parented to the player, the AI will move in relation with any movements that are happening on the player.
Yes, I do recommend it. I have been doing it for a few years now, with no performance problems, and if there is overhead I'm guessing it is decimal. You can do it that way, or you could set the vector's y axis up a bit. The reason I always pick this method is because you can set the exact position that the AI will look within the GUI and don't need to tweak the axis till it's perfect, plus if you choose to scale the object or rotate it or whatever, the Look Point will remain at the same relative position.
So far it only moves in relation to the player.
The AI is not parented to the player.
Yeah it did sound like a good Idea when I first read it, Thanks for that :)
I'll try out some of the methods you suggested right now ^^
Unfortunately none of these worked :(
I'd like the AI to slowly approach the player at all times.
Answer by Exalia · Sep 17, 2013 at 11:37 AM
Thanks for all the help everyone I eventually fixed it by using the script :
gameObject.transform.LookAt(target.transform.position);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
You could tweek the move speed and rotation speed values
If you're not slerping the rotation anymore, then that is exactly what I said :) So removing the slerp is just like an infinity speed value.
Good that it's working now.
Answer by robhuhn · Sep 16, 2013 at 10:49 AM
The AI can only turn while going forward (like a car) and can't turn on the spot?
Not sure if I get you right but slerping the rotation with a given rotation/move speed ratio could result in a large turn radius, then it will never reach the target in some cases.
You could tweek the move speed and rotation speed values or set the rotation and position independently.
Sorry I don't understand a lot of this, First I'm not sure if you're answering or asking a question. Secondly you mention that it is possible to result in a large turn radius, could you explain this a bit more? :)
I'm not sure if I'm tired or missing something but
I have a player I have an enemy
I want to slowly reduce the distance between the player and the target I want to make the enemy rotate and look at the player
That's all
I asked a question and gave a possible solution ;)
Did you try changing the move speed while the enemy gets closer?
//distance replaces move speed in this case
transform.position += transform.forward * distance * Time.deltaTime;
I like the change of speed that occurs but it doesn't stop the AI not wanting to be looked at, If I turn my character all the AI spin round and look at it's ass constantly, and If I walk too close to them they spin round to the back.
None of the suggestions work I'll repost the script I am using.
AI will get to a certain distance and then flash or spin round my Player GameObject :(
void Update ()
{
distance = Vector3.Distance(target.transform.position, transform.position);
transform.rotation = Quaternion.LookRotation(target.transform.position - transform.position);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
I tested the script with my solutions and it works fine. $$anonymous$$ay be you have another scene set-up.
Answer by ibrahim091234 · Sep 22, 2021 at 06:50 PM
man this is so amazing we dont even need a nav mesh agent cool man
i support this
Your answer
Follow this Question
Related Questions
Horror Game AI script recommendation? 1 Answer
Follow script Error NAN? 0 Answers
AI Script Problem, Horror Game 0 Answers
Unity3D AI using animations 1 Answer
Enemy Follow Player 1 Answer