The question is answered, right answer was accepted
I'm trying to implement an object that will follow behind my player AND rise and fall as my character jumps and land, drops in descent, etc.
The AI is currently using a character controller as collider and is able to follow the player's transform with an offset, but when the player is standing still or jump and matches the AI object's Y, the AI bounces up and down. I am currently using a character controller and characterController.Move(). He is not being affected by gravity.
Here is my current code:
//If he isn't flinching, he needs to begin his pursuit
//Normal AI follows player when in range and attacks when close enough
if(activated && aiType == "Normal")
if(activated && aiType == "Observe")
//Define his movement....
var movement = moveDirection + Vector3 (0, 0, 0);
//...then move him (order of operation :P)
controller.Move(movement * (Time.deltaTime));
//transform.position.y = player.transform.position.y;
if (transform.position.y < player.transform.position.y + 2)
controller.Move(Vector3(0, player.transform.position.y ,0)* (Time.deltaTime));
if (transform.position.y > player.transform.position.y + 2)
controller.Move(Vector3(0,-player.transform.position.y ,0)* (Time.deltaTime));
if (transform.position.y == player.transform.position.y + 2)
transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);
var direction : Vector3 = player.transform.position - this.transform.position; //target aquired
//If something is in my sights
if(direction.magnitude < awareness)
direction.y = 0;
if (direction.magnitude > 1f && activated) //if we are away from the wandering point
Debug.DrawRay(transform.position+Vector3(0,.1,0), transform.forward, Color.black);
transform.forward = (transform.forward * 0.9f) + (direction * 0.1f);
controller.Move((transform.forward * maxSpeed ) * Time.deltaTime);
Is there a more effective way to make this AI and still retain his solidity?
Answer by Hendrys
Jan 18, 2012 at 05:05 AM
Check if your AI character has a Character Controller this kind of component reacts to gravity, so when your AI tries to "reach" the player it inmediately falls down. And it tries to reach the player agaian and falls ans goes on and on. That's the reason of the Bouncing.
Try to elliminate the gravity reaction from your code. And do you really need a character controller? why no trying a regular Collider? doesn't that fit your requirements?
I also think you don't need the last three if's in your Update function. Remove them.
My AI component uses a character controller, and no, it's not affected by gravity. A regular controller could work as well.
The last three If's are the only reason why he lifts in the first place.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
5 People are following this question.
Planet Physics AI Follower
Making water physics
How to create a basic follow AI
How to get rid of jumpy collisions?
Using more precise floats for movements.