- Home /
Enemy Rigidbody Not Responding to Gravity
So I'm currently prototyping an enemy AI script (enemy model is just a red cube that is 2 units high, and 1 unit wide and long).
The enemy gets spawned through an empty game object. His first function is to patrol among several Waypoints (borrowed script from a tutorial). If he finds the player, he then calls a function to rotate and move toward the player. Once he's in a certain range, he then calls a function to attack.
Now I've had plenty of issues with that if statement, but the problem is his patrolling function. He was working before I commented stuff out to make him stop patrolling for fancy screen shots, but now he is no longer functional. Sort of.
When his prefab is spawned, he does nothing. It prints to tell me that he's started patrolling, but he's not even dropping to the floor (he spawns a unit above the floor and gravity is supposed to bring him down for him to patrol, which worked before like I said). Now he just floats at his spawn point, but his patrol function is being called.
After some troubleshooting some stuff, I finally disabled the spawn point, and brought the prefab directly in the scene. Oddly enough, the enemy will ONLY WORK CORRECTLY if I hit play while he's located within the range (a radius) that's defined as finding the player (so if he's close enough to the player). If I press start while he's farther than that range, he remains floating in space as before, having the same issue.
This is what is applied to the enemy prefab in the inspector:
Cube (Mesh Filter): Default Settings for a cube
Mesh Renderer: Default Settings
Character Controller
Slope Limit: 45
Step Offset: 0.3
Skin Width: 0.08
Min Move Distance: 0
Center: X:0, Y:0, Z:0
Radius: 0.5
Height: 1
Enemy AI (Script): My applied script
Rigidbody
Mass: 100
Drag: 0
Angular Drag: 0
Use Gravity: True
Is Kinematic: False
Everything else is default, except all rotations are checked for frozen.
And here is the entire code. I'd provide snippets instead, but I just don't know what it is that is causing the problem. Also, I'm not too familiar with the Couroutine and While statements, so if something can be done better, please let me know. Thanks
I saved it as a text file for anyone that would like to view it. enemyAI.txt
Answer by aldonaletto · Mar 19, 2012 at 09:14 PM
This script uses SimpleMove, which takes care of gravity for you - but there's no gravity when SimpleMove isn't being called.
You can try to add a SimpleMove(Vector3.zero) to the Update function - it will not move the character, but the gravity will be applied.
Insert this instruction in the first line of Update():
GetComponent(CharacterController).SimpleMove(Vector3.zero);
NOTE: Rigidbodies and CharacterControllers aren't a good combination. Delete the rigidbody or, if you really need it, set Is Kinematic to avoid weird effects.
really, thank you so muck Aldo! I've been looking for something to help me aswell, with the same problem (but i don't use a rigidbody). This stopped my characters from falling through the ground and from flying while trying to chase me! thanks a billion :p