- Home /
How do I check if an Object isn't moving? (C#)
I'm currently making the AI for my game, or the prototype AI, and I'm using the mechanim animator to animate it, so I'm making a boolean to check if the opponent is moving or not, so the animator will know what animation to play.
I'm having a problem with that, I'm checking if the opponent isn't moving by using a "previous position" value and the print function to check if it's working.
It isn't, even when I get to print both positions, and it's exactly the same, it still recognises it as moving.
Here's the code:
void Update ()
{
if (myTransform.position == previousPosition)
{
moving = false;
print("IT ISNAE MOVIN!");
}
else
{
//moving = true;
print("IT'S ALIVE!" + transform.position.ToString() + previousPosition.ToString());
}
Debug.DrawLine(target.transform.position, transform.position, Color.red);
//Look at target
if (gameObject.tag == "Kruncanite")
{
myTransform.localRotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(new Vector3(target.position.x, 0, target.position.z) -
new Vector3 (myTransform.position.x, 0, myTransform.position.z)), rotationSpeed * Time.deltaTime);
}
else
{
myTransform.localRotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position -
myTransform.position), rotationSpeed * Time.deltaTime);
}
//Move towards target
if(Vector3.Distance(transform.position,target.position) > followDistance)
{
myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
}
if (gameObject.tag == "Kruncanite")
{
kruncaniteAnimation.SetBool("Moving", moving);
}
previousPosition = myTransform.position;
}
What am I doing wrong?
Answer by Seth-Bergman · Jan 07, 2013 at 08:48 PM
Well, you are changing the position right BEFORE the check:
myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
Unless "moveSpeed" equals zero, this changes your position ALWAYS;
if (myTransform.position == previousPosition)
is therefore always not met. Even if you are at the same position as the target, using this method will still move FORWARD.
You can add somethiong like this:
float followDistance = 1.0;
if(Vector3.Distance(transform.position,target.position) > followDistance) //just add this line to your code
myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
...etc
this way, once you are close enough, you stop moving
the point is you need to update the movement code so that the character actually CAN stop moving!
Alright, I've moved it to the top, and it's flagging it as false sometimes, but when when the enemy is at a standstill, it keeps switching from true to false.
The code you provided me to move the object doesn't work as well as what I had, most of the time, the enemy would just move in one direction and only sometimes change, either that, or it would stay on the same spot and keep switch between 2 directions.
yeah, sorry, I didn't really think that would work as written , it's more of an example..
here's one that will actually work :) :::::::::
float followDistance = 1.0;
if(Vector3.Distance(transform.position,target.position) < followDistance) //just add this line to your code
myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
...etc
this way, if you're close enough, you don't move..
I'll edit my answer, give me a few..
Okay, I've actually added the character I want to make an A.I. (I tested it on boxes before), but they don't even move or turn when I use the script, do you know why that might be?
Sorry I had < ins$$anonymous$$d of >
It should work now (fixed the answer)
Now I'm having another problem, the character keeps moving to the left, the updated code is on the original question.
Your answer
Follow this Question
Related Questions
How do I make the crosshair go closer/get bigger if an item/wall is close to you? 2 Answers
C# GUI.Button Transform.Position 1 Answer
How to make cubes between 2 point and stick them together? 0 Answers
Simultaneously moving GameObjects using C# script. 3 Answers
My AI keeps flickering between stopping and moving. 1 Answer