- Home /
No gravity with mecanim...?
Hi! First, sorry for my english!
I just started to play with unity. I really like it. I was doing several tutorials. Now i trying to make a 3rd person multiplayer shooter, with mecanim.
My animations works well, and after 10 hours it is syncronized with the other clients. Awesome! but I have a very irritating problem: the gravity.... (I think)
Here is the video about it: http://youtu.be/o1-MXXpq2NA So the main problem is that the character dont fall down, and can run in the Y axis. The other problem is, if I look down or up with the mouse, the character leans that way... Any idea how could I solve this?
Solution: I changed in the Physics Manager (Edit/Project Settings/Physics) the gravity (Y axis) from -9,81 to -50.
Answer by TonyLi · Jun 04, 2013 at 04:11 PM
I can't make out all of the details of the Hierarchy and Inspector tabs, so these are my best guesses. Can you provide a screenshot of the fully-expanded components in the Inspector tab?
Gravity: Does gravity work without the animator component? Try disabling the animator component and any scripts that rely on it. Then press Play and, in the Scene View, raise the character off the ground. The rigidbody (with Use Gravity ticked) should make the character fall.
Check the layers, including any colliders attached to any child objects. It's possible that a child object is colliding with the character's capsule collider, preventing the capsule collider from falling.
Camera: Keep the camera and character separate. Don't make one a child of the other. If one is a child of the other, then when the parent's rotation changes, the child will also rotate.
For both of these issues, examine the scripts provided with the Mecanim Examples project (available on the Asset Store). Compare your scripts to see the differences.
Hmm... Yes, if I turn off the Animator component, the character falls down properly. $$anonymous$$ore specifically if I turn on this component, and I turn off the "Apply Root $$anonymous$$otion" option, it works well. Sadly it is not yet a solution, becouse if I do so, my character control won't work.
Okay, we're narrowing down the problem. :-) Try these ideas:
In the animation importer, under Root Transform Position (Y), tick Bake into Pose and select Based Upon > Original. Then click Apply.
If that doesn't work, your animation file might have a problem with gravity, which is setting Animator.gravityWeight wrong.
Finally, consider temporarily switching to a CharacterController ins$$anonymous$$d of a CapsuleCollider+Rigidbody. If this works, then maybe the Animator isn't playing nicely with your rigidbody setup.
Ok. I have 2 kind of animations: Raw $$anonymous$$ocap data for $$anonymous$$ecanim and from the tutorial video. Now in the Animator, I only got from the second one. I modified all these animations RTransform Position Y as you said.
During this time, I solved my camera problem. I had a $$anonymous$$ouseLock script and forgot to change from xy direction to only x. So now my character don't leans down or up.
Back to the gravity: I upload one more video: http://youtu.be/wzDJeaG60dA
Gravity is working.. but not really as I want :D
I checked the Animator.gravityWeight and it was 1... btw I don't really know, if its good or not... in the tutorial scene, it is 1 too... $$anonymous$$y check was:
Debug.LogWarning(anim.gravityWeight);
Did I this well?.. :)
I tried change to character controller. I removed the BotControlScritp.cs, the Capsule Collider and the Rigidbody. And add the default fpsinput to the character prefab. Gravity was good, but withouth any animation...
I faced a similar problem in using the additive move-forward animation with gravity (http://answers.unity3d.com/questions/442277/mecanim-and-rigidbody.html).
In my case, I was try to animate a simple legless NPC, and I used Generic $$anonymous$$ecanim for it, I tried almost all possible setting but none of them works. However, when I play with the example provided by Unity, I noticed that the gravity is not working once I switch from Humanoid animation to Generic animation, it is possible for the gravity to not work in Generic mode?
Answer by Jeff-Kesselman · Oct 25, 2013 at 09:57 PM
I had this problem.
Make sure "apply root motion' is NOT checked in your animator.
If it is then I think the animator applies the in-place position after every frame and puts it back to previous Y. Anyway, unticking it solved my problem.
Answer by vet-cat · Aug 31, 2014 at 03:17 PM
add CharacterController add script ApplyGravityCharacter.cs
using UnityEngine; using System.Collections;
public class ApplyGravityCharacter : MonoBehaviour { public float Gravity = 20f; public bool UseGravity = true; private CharacterController characterController;
void Awake()
{
characterController = GetComponent<CharacterController>();
}
void Update ()
{
if (!characterController.isGrounded && UseGravity)
{
characterController.SimpleMove(gameObject.transform.up * Gravity * Time.deltaTime);
}
}
}
Thanks vet-cat, i've never give much attention to the character controller, and i was trying to create an ragdoll character but i was having a problem to apply the gravity on the character, because i was trying both add an rigidbody to the main object and he always fall through the ground and add an collider and the character was rotating like an retard, but adding this script managing the character controller i was able to apply the gravity to my ragdoll character and changing from mecanim to ragdoll easily!! thanks again!!
As I am not able to vote yet, I gotta say, thank you very much for this easy solution. I was mixing root animation with controller.move, which did weird things. No it floats perfectly over the ground ;)
Answer by Anton1274 · May 14, 2015 at 03:39 PM
Yeah the problem is with the Animator Apply Root Motion, if is checked true there is no Gravity over the RigidBody if is set false the Gravity will affect the RigidBody. Check if the Collider is Grounded and modify this property
Answer by Jon Jennings · Jul 06, 2015 at 05:59 PM
I changed my routine which updates the root motion, such that it only applies the X and Z components of the root motion and leaves the Y component as applied by the ridgidbody
void OnAnimatorMove(){
tempvector = player_rigid_body.velocity;
tempvector.x = anim.deltaPosition.x / Time.deltaTime;
tempvector.z = anim.deltaPosition.z / Time.deltaTime;
player_rigid_body.velocity = tempvector;
player_rigid_body.rotation = anim.rootRotation;
}