- Home /
Physics gravity appears very weak
Hello,
I have a character which doesn't fall at a reasonable speed under gravity, rather he floats down to the ground. A fall which should take around 1 second, takes nearer 30s. Given the limitations of the physics engine (e.g. ignores air resistance) I would expect falls to be quicker not slower, so this is very odd.
Gravity is currently set to -9.8m/s2 (on y axis) which is correct. Altering this value is one way to solve the problem but it seems unlikely to me that Unity would ship with gravity out by 1-2 orders of magnitude - much more likely to be something I've missed.
I've read the docs a few times through and have read (I think) all of the existing posts on this matter but have yet to solve my problem. I've listed some of the specific things that I've tried/checked.
The drag is set to 0.001, although I've also tried zero.
The size of my character model is just under 2m tall. Picture 2 shows the character hovering next to a default cube - hopefully proof that the scale is indeed correct.
The scale settings of all parent (and child) nodes of my character are 1 across the board.
I've played with the mass of the rigidbody although (thankfully) this had no affect.
I removed the animations from the animation controller incase something was upping the y-value
I checked the Time.timeScale value is 1
Can I anyone point me to something that I may have missed please?
Picture 1: The character "tallguy" with the settings for the animator, rigidbody and capsule collider.
Picture 2: The character positioned next to a 1m3 default unity cube to show the scale of the model
I added a script to the cube to see how a basic gravity calculation should look. I work out the speed since the level started
velocity = (gravity * timeElapsed^2) / 2
I'm not convinced that formula is right but the cube now falls beautifully. The character is still easing down like a spaceman - arghh! So the physics in Unity is calculating a massive drag factor from somewhere, but I just can't figure out where!
The entire script I used is:
void Update()
{
float g = -9.81f;
float speed = 0.5f * g * $$anonymous$$athf.Pow(Time.timeSinceLevelLoad, 2);
float dist = speed * Time.deltaTime;
transform.position += transform.up * dist;
}
Added a RigidBody to another cube and hey presto it falls fine.
So then tried disabling the Animator on my character - and hey presto - it now falls properly.
Still need to work out why the animator is affecting the transform in this way.
I now think this is related to: http://answers.unity3d.com/questions/468709/no-gravity-with-mecanim.html
You shouldn't use transform.position in conjunction with a rigidbody. Use rigidbody.$$anonymous$$ovePosition or AddForce ins$$anonymous$$d. If your object is moving very slowly there is a strong chance that there is something setting transform.position or altering velocity repeatedly.
Animations also have the power to modify position. A Legacy animation will try to hard set a world position if you use it in conjunction with $$anonymous$$ecanim Animator and statemachine. A $$anonymous$$ecanim compatible animation has Root $$anonymous$$otion access, usually, allowing it to modify 'root' position.
Check the $$anonymous$$odel importer for the animation tab and play with the position Bake settings, see if this changes the situation.
Hi meat5000, thanks for taking the time to respond.
You shouldn't use transform.position in conjunction with a rigidbody.
Yes you're quite right to point that out. In this case, my script was applied to a Cube that didn't have a rigidbody so that I could see how it should fall. I then created a second cube with just the rigidbody (no script), and ofcourse that fell to earth fine as well. Hence leading to the Animator/mecanim ...
Animations also have the power to modify position. A Legacy animation will try to hard set a world position if you use it in conjunction with $$anonymous$$ecanim Animator and statemachine. A $$anonymous$$ecanim compatible animation has Root $$anonymous$$otion access, usually, allowing it to modify 'root' position. Check the $$anonymous$$odel importer for the animation tab and play with the position Bake settings, see if this changes the situation.
I think you nailed it here, and this was mentioned in a number of the posts I'd read but I had 2 further niggles which prevented me to getting the solution.
I had to realise that these bake root transform settings were per-animation and not global. (silly me!)
I had to realise that the no-gravity problem exists if there are no animations on the AnimationController. (I suspect this latter one is a bug.)
Answer by karlhulme · Oct 25, 2013 at 01:49 PM
I've found the following resolved the problem for me...
Firstly: you must have at least one animation on the AnimationController which is referenced by the Animator.
In my attempts to narrow down the problem I had removed all the animations in the hope of proving it wasn't the animations at fault. In this case, if you have no animations, then you will get the slow-fall affect. I suspect this behaviour is a bug in Unity as gravity should still apply. If gravity were applied when no animations were on the Animator Controller, it would make it quicker to ascertain that the animation is at fault in some way.
Secondly: Under the animation import settings, I selected the animation added to the Animator Controller. In this case "Idle". I then baked the Y transforms of the root position into the pose.
My character is now falling to earth with a bump. FYI, I'm using the male character pack by 3DRT.com.
Hrm , can your screenshot your Animation Controller for me? It is the JS script you are referring to? Or the State-$$anonymous$$achine Controller file? Just nailing down the ter$$anonymous$$ology :P
The first image above is the 'State $$anonymous$$achine'. You can add parameters to this and use those to control you animations via script. The file produced as a result is the Animator Controller. Try not to mix the two animation types, Legacy and $$anonymous$$ecanim. (For an example of legacy, load AngryBots tutorial).
But yes, I imagine animation will have a hard time if there is no animation to reference and the 'Apply Root $$anonymous$$otion' box is checked in the 'Animator' Component.
Hi again,
I feel I'm missing something...
The first screen shot is my "Animator Controller". e.g. this is what I get if I click Assets > Create > Animator Controller. Unity has implemented this as a state machine. So for me, Animator Controller == State machine. (I did refer to it as the Animat*ion* Controller - which is I see is wrong)
I'm not clear what you want me to screen shot. I don't have any user created script files.
(I know I'll need one for interacting with the animator, but for the purpose of resolving my gravity problem I removed them and just used a default animation.)
I didn't think I'd referred to anything Legacy???
That's good, no need for screener. I wanted to check your weren't mixing methods :)
And yes your ter$$anonymous$$ology is fine :)
Animator Window shows the State $$anonymous$$achine of the Animator Controller which is used in the Animator Component :D Although to be a pedant Animator Controller != State machine as you can't drop the state machine itself into the Animator Component, only the Controller file it produces.
Be aware that in it's base form, Animations are only subject to Exit Time transition parameter. Try changing this to something else or adding another parameter and see if it has the same effect.
Hi, thanks for all your help. If you wanted to convert your very first comment to an answer, I'd be happy to accept it.
Hi $$anonymous$$arlhulme
I have a similar issue to you but unfortunately I cannot resolve it.
I have a character with "generic animation type" rig and I have set all animations to tick "root transform position (y) bake into pose". On the game object I have a rigidbody with the following:
$$anonymous$$ass = 120
Drag = 0
Angular drag = 0.1
Use gravity = tick
Is kinematic = not ticked
Interpolate = none
Collision detection = discrete
Freeze position = unticked
Freeze rotation = ticked
The game object also has an animator attached, with an animation controller that has the animations specific to the model of the character I have created (which have the root transform position Y bake ticked). The state machine had quite a lot of transitions and such, so I tried creating a new one with just the idle animation, like your picture above, but still no luck. There is also a C# script, but I have disabled that for the time being to get the gravity to work. But I put in there a print to test what animator.gravityWeight was and it was saying 0, even though root transform position Y bake ticked.
In the physics settings gravity is set to -9.81.
Whilst the root game object does not have a capsule collider or any type of collider, I have given some of its children sphere collides. But deleting them doesn't resolve the issue.
So what could be wrong? Any ideas? Thanks
Answer by amin_alizadeh · May 06, 2014 at 05:24 PM
You can cast a ray from the character to the ground and adjust it using Animator.MatchTarget function.
void Awake() {
Animator anim = GetComponent<Animator>();
}
void Update() {
// Raycast down from the center of the character..
Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(ray, out hitInfo)) {
// ..if distance to the ground is more than 0.5f, use Match Target
if (hitInfo.distance > 0.5f){
// MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray.
anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 10, 0), 0), 0f, 1f);
}
}
}
If MatchTarget does not work as you wanted, you can also replace it with:
this.transform.position = hitInfo.point;
This will ensure that the character goes on the ground.
Your answer
Follow this Question
Related Questions
How to make a object jump constantly at y and move to the next position to z (perfectly) 0 Answers
How can I turn on/off Rigidbody.useGravity? 0 Answers
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
How can I match Unity's gravity implementation per object? 1 Answer
My objects keep falling through 1 Answer