Trying to get AI to follow who ever is closer
so im trying to get my enemy AI, to follow who ever is closer but all he does is just sit there...
SCRIPT:
JAVASCRIPT
var target : Transform; var target1 : Transform; var moveSpeed = 3; var rotationSpeed = 3; var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform;
function Awake() { myTransform = transform; }
function Start() { target = GameObject.FindWithTag("Player").transform; target1 = GameObject.FindWithTag("Player").transform; }
function Update () {
var distance = Vector3.Distance(myTransform.position, target.position);
var distance1 = Vector3.Distance(myTransform.position, target1.position);
if(distance > distance1)
{
if (distance<=range2 && distance>=range)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance<=range && distance>stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
if(distance1 > distance)
{
if (distance1<=range2 && distance1>=range)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance1<=range && distance1>stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
}
if any one can please help me it would be very helpful
Answer by dhore · Dec 11, 2015 at 04:57 PM
You only seem to be changing the rotation of the object, if you actually want it to follow the other object then you need to be changing the position.
Note: when changing the position you can use Vector3.Slerp
just like you are currently using Quaternion.Slerp
for the rotation
Sorry, my bad on the wording.
Vector3.Slerp takes 3 parameters:
1) a Vector3 starting position
2) a Vector3 ending position
3) a float for how much to move on that frame (ie. the speed)
I'm not catching on... Could you rewrite this for me because I'm not understanding how to word it
Sorry... I agree that could've been worded a lot better... I've also just noticed that your middle if statement for your range checks does actually include a transform.position change (not sure how I missed that last time..).
Looking at it again though, I think I've found your error:
function Start() {
target = GameObject.FindWithTag("Player").transform;
target1 = GameObject.FindWithTag("Player").transform;
}
The function GameObject.FindWithTag
will return the first GameObject that it finds with that tag. Therefore no matter how many times you call it, it will always return the exact same GameObject. So your Start function has initialized target to be equal to the same object as target1.
This is where your issue begins; because then when you calculate distance and distance1 you are using the same GameObject and therefore both of the distances are equal to each other. And your code does nothing at all because you've set it up to only do something when one distance is greater than the other. Your code never does anything when they are equal.
so should I create my own tags called Player1 and Player 2 and tag target as Player1 and target1 as Player2, would this work?
Answer by ImThatGuy · Dec 14, 2015 at 04:10 PM
pragma strict
var target : Transform; var target1 : Transform; var moveSpeed = 3; var rotationSpeed = 3; var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform;
function Awake() { myTransform = transform; }
function Start() { target = GameObject.FindWithTag("Player1").transform; target1 = GameObject.FindWithTag("Player2").transform; }
function Update () {
var distance = Vector3.Distance(myTransform.position, target.position);
var distance1 = Vector3.Distance(myTransform.position, target1.position);
if(distance > distance1)
{
if (distance<=range2 && distance>=range)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance<=range && distance>stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
if(distance1 > distance)
{
if (distance1<=range2 && distance1>=range)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance1<=range && distance1>stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
if(distance==distance1)
{
if (distance1<=range2 && distance1>=range)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance1<=range && distance1>stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
}
this is my script now but same problem, the enemy does not move
i added it where if the 2 players are the same distance apart to just go after player2
Rather than copying and pasting the whole code and putting in another if statement, you could just use greater than or equal to like so: if(distance1 >= distance)
and you should also be using else if
for the second distance check as you'll never get a case where both evaluate to true.
I also just noticed a little typo that might be causing you some problems:
if(distance1 > distance)
{
if (distance1<=range2 && distance1>=range)
{
// rotations
}
else if(distance1<=range && distance1>stop)
{
// rotations
}
else if (distance<=stop) // <<----- wrong distance variable
{
// rotations
}
}
You are using the wrong distance variable in the last else if. That would be causing either $$anonymous$$or or major problems for you.
Other than those few things though, you're looking pretty good :) Hope it all works now!
new problem now the enemy is looking back and forth at both of the players and staying directly in between us, i assume it has something to do with the if(distance==distance1)
And between if(distance > distance1) and if(distance1 > distance) you want me to place a "else if"
What I meant was:
if (distance > distance1)
{
// do stuff
}
else if(distance1 >= distance)
{
// do other stuff
}
pragma strict
var target : Transform; var target1 : Transform; var moveSpeed = 3; var rotationSpeed = 3; var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform;
function Awake() { myTransform = transform; }
function Start() { target = GameObject.FindWithTag("Player1").transform; target1 = GameObject.FindWithTag("Player2").transform; }
function Update () {
var distance = Vector3.Distance(myTransform.position, target.position);
var distance1 = Vector3.Distance(myTransform.position, target1.position);
if(distance > distance1)
{
if (distance<=range2 && distance>=range)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance<=range && distance>stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
else if(distance1 >= distance)
{
if (distance1<=range2 && distance1>=range)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance1<=range && distance1>stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance1<=stop)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
}