Hi,
I’m not finding case 863707 in the bug tracker anymore so I’m not sure what status this bug is in.
Any chance this will be fixed in 5.6? I’m curious if the fix isn’t coming soon as I’m deciding if we need to rework systems so we can continue development. It is new to 5.5 but 5.4 had other issues that blocked us so I can’t just roll back sadly.
Hi hoesterey,
Thank you for the bug report. There hasn’t been set a target version for a fix yet. It seems unlikely at this point, that there will be a fix within the 5.6 Beta cycle.
Ok, thanks for letting me know though it wasn’t the news I was hoping for.
Looks like I’ll need to hack the system to use Forces for exact movement. I think the following code will work for now. I’ll have to test it.
//Rotation:
var x =Vector3.Cross(currentDir.normalized, newDir.normalized);
float theta =Mathf.Asin(x.magnitude);
var w = x.normalized * theta /Time.fixedDeltaTime;
var q = transform.rotation * rigidbody.inertiaTensorRotation;
var t = q *Vector3.Scale(rigidbody.inertiaTensor,Quaternion.Inverse(q)* w);
rigidbody.AddTorque(t - rigidbody.angularVelocity,ForceMode.Impulse);
//Movement
//hopefully I can just use a force with ForceMode.VelocityChange. we will see.
Heya,
Thinking about this more. I’d like to give a simple use case (that is much simpler then mine) as an example of how much trouble this could cause your community. The way I wrote the bug it sounds like an obscure issue and I just wanted to ensure its understood that this could cause widespread issues. In my game I can probably get around the issue by using just forces. Others may not be able to as easily.
Mario like Platformer (I’m not making a platformer but this is one of the simplest games to make)
-Developers will probably want to use .MovePosition() for controlling the character. As forces tend to be too “sloppy” for arcade style controls.
-The character jumps onto a “steam vent” a force is applied to push the character up.
-the bug causes the character to become unresponsive to player control.
I’ve replied to the case opener directly, but just to make more information public…
It appears to me that this isn’t a bug really but more of a misunderstanding that even though the force itself acts for one physics frame, the effects of it last longer (e.g. angular and linear velocities will still be != 0 for a few frames depending on the drag settings). Thus, having code that sets the transform directly (this is what MovePosition/MoveRotation do when the body isn’t kinematic) while at the very same time physics controls the body is bound to expose behavioural artefacts.
I’m not sure I agree that it’s not a bug. This test case was simply to show the issue and not the only use case.
Let me explain:
-I’m not adding force every frame. I am only adding the force once. However the object never stops jittering if force is applied once and you apply move position every frame after… I think you have an issue where the force is unable to falloff or stop applying if a move or rotate position is applied. The two interpolations are fighting each other incorrectly.
An example use case:
Making a game like Mario brothers I would probably use MovePosition to set the position of the character as adding forces would be a bit too sloppy.
E.g. transform.MovePostion(transform.position += new Vector 3(1,0,0) * time.Deltatime); //move right
Then In Mario I walk into a steam vent that adds a force that pushes him up and to the left.
Mario begins to jitter, even after leaving the steam vent. He will NEVER stop jittering until the player stops pushing a direction, even after leaving the steam vent that is applying the force.
The fact that he would never stop jittering sounds like a bug no? It seams that the force applied once is lasting forever.
I could simply “MovePosition” manually when he walks into the steam vent. But based on what your telling me it would be bad to ever use forces with MovePosition. I’d expect to have an order of operations that allows these types of interactions.
Do you recommend only using forces or only setting positions?
P.s.
Based on your response I think I see what your doing under the hood. While I still think you may have a bug based on the fact that the force never stops jittering (see previous post) I’d like to make a suggestion:
I expected that under the hood you did something like this:
//if active force and moveposition is passed in for this body.
Vector3 goalposition = Rigidbody.position + ForceDeltaOffset + MovePosition;
MoveRigidBodyWithPhysics(goalPosition);
I’m guessing this is not the case.
Without digging though the source its hard to know what your doing under the hood. Unless I’m missing something (possible) the only way to get the above type of interaction currently is to role your own forces and apply those forces as: MovePosition(offset + force)?
Or to simply do everything with forces. (I’m guessing adding a force with a forcemode of velocity change for movement and than an opposite force with a different force mode would accomplish something similar to what I’m looking for no? Only problem is that I’d then need to apply my own counter force for drag assuming I wanted that object to behave differently when impulsed vs. controlled by a player. (Mario stops instantly when the player takes his hand off controls but not when he is impulse by a level object.) Though its doable.
If you don’t have functionality it would be good to simplify this use case and get it closer to the physics engine where it will be more performant. E.g. Rigidbody.MovePositionWithForceApplied(); that does something like this:
Let me explain how this type of interaction actually works under the hood.
MovePosition / MoveRotation is designed with kinematic bodies in mind (Rigidbody component with “isKinematic” checked). When used with kinematic bodies, it sets the so called ‘kinematic target’ in PhysX. Later on, during the FixedUpdate stage, the physics engine would work out the speed required to move the body that long over a single frame. But again, that only happens with kinematic bodies. Once MovePosition / MoveRotation was called on the non-kinematic body, there is a simple fallback involved that is just overwriting transform to the physics engine directly with no extra calculations. An important note here is that if you call MovePosition / MoveRotation during Update() cycle on a non-kinematic body, the transform is being written to directly so it’s pretty much the same effect as writing to Transform from C#.
Another observation is when you set the force with an AddForce variant, the effect is being delayed for one frame as we set the force on the body but it’s only going to be applied during the FixedUpdate stage. The force acts for only one frame until getting reset, but it produces velocities that will be non-zero for some frames depending on the environment / drag / friction settings.
At this point it should be clear that the jitter observed basically comes down from the fact the transform is being controlled independently from two places at the same time: MovePosition / MoveRotation and the physics simulation. The physics simulation would apply the transform adjustment based on the velocities times delta time every Time.fixedDeltaTime seconds whereas you overwrite that every Update via MovePosition / MoveRotation.
Now, what I’d think as a possible way to rule out the control conflict is to come up with a way to stick to either forces-exclusive code or MovePosition/MoveRotation-exclusive code. Otherwise, it’s just bound to be overwriting the result of each other I believe.
Yes I understand what your saying thanks for taking the time to explain Yant!
It would be fantastic to have a function that allowed for you to additively offset a rigidbody without interfering with applied forces. Something you may want to consider adding to a future version of Unity as it will make implementing this type of common scenario much faster on the developer’s side of the fence.
For now, we will move to using just forces.
Thanks for the help and clarification, I really appreciate the support!