Jump to content


Photo

My issue with physics and movement


  • Please log in to reply
1 reply to this topic

#1 DIGI_Byte

DIGI_Byte

    Unitologist

  • Hosted
  • 1,747 posts
  • Location:Australia
  • Projects:GTFO
  •  Everybody do the Flop!

Posted 10 April 2013 - 03:07 AM

Hey, i have some issues that have been plaguing my mind with problems and i'm after some guidance or understanding

but in unity with dealing with movement, specifically 'transform' and physics

straight off the bat I'm really cautious with using transform as from old game experience, it would simply +/- a value from its position/rotation
(TL:DR, scroll down to bold)


to the naked eye it wont be much noticeable but in some situations it can be unnerving and hopefully it isn't an issue in Unity as it has been in other simple engines

ok, so onto the meat of the subject

using transform to my knowledge forces an object to move to an offset to its position
now, is it actually moving? or is it "teleporting"?
you may notice in some games when a object or item 'teleports' or respawns that there is sometimes a mid position
example would be a player respawning and you for a brief fraction see them flash across your screen
I believe that's simply a side effect of resetting position to a new location, in other words altering the 'transform'

I digress,
The main issue is that if you have a simple physics object and apply a transform which is an override on its position, what stops it from pushing through other objects?
naturally we would use force instead? but is it always the right way to do things?

an issue i tried dealing with was a custom physics and control system for a side scroller game in Unity
using a constant transform to push them down when they are not grounded
and when grounded to stop applying the transform gravity
because of the nature of the game, it felt right to use translate to emulate that old school feel of simple physics

I faced an issue with the trigger when walking between tiles to determine if it was grounded or not
so, i used a character controller which could detect if it was colliding bellow and above which was nice

Unfortunately, applying the character controller stopped my rigid gravity to work
(using transform gravity it didn't detect contact with the ground and kept falling)

I guess what I'm trying to get at is...
is it safe or wise to use transform on objects with physics, is there a risk of the object being pushed through another physical object because of the transform? or should we just use constant force as an alternative?


 


Edited by DIGI_Byte, 10 April 2013 - 03:12 AM.


Lyon-Fixed.gif
RIP 2323


#2 Nertea

Nertea

    ...lo sa raptor!

  • Hosted
  • 3,349 posts
  • Location:Vancouver, Canada
  • Projects:Star Villains and Space heroes, The Dwarf Holds
  •  T3A Chamber Member
  • Division:BFME/Unity

Posted 10 April 2013 - 05:43 PM


 

ok, so onto the meat of the subject

using transform to my knowledge forces an object to move to an offset to its position
now, is it actually moving? or is it "teleporting"?
you may notice in some games when a object or item 'teleports' or respawns that there is sometimes a mid position
example would be a player respawning and you for a brief fraction see them flash across your screen
I believe that's simply a side effect of resetting position to a new location, in other words altering the 'transform'

 

Not really. It depends on the engine. You often see that in an RTS because of how exactly modern RTSes work (look up lockstep simulation for more info).

 

In general, for the rest of the question:

 

Unity's physics solver (and most other engines') runs at a fixed interval (which you can define in the physics settings). If you do anything at all between those ticks (ie, every FixedUpdate), they will be independent of the physics calculations. So if you moved an object into another object during an Update tick, the physics solver would not read the collision until slightly later, and then it might have some strange issues.

 

To be safe, with an object under full physics control (ie, non-kinematic), you should avoid ever setting any properties of the Transform component. There's no harm in reading them though. You should always set these properties through the Rigidbody component (eg. rigidbody.position and rigidbody.rotation). These are the same as using transform versions, but are instead delayed until the end of the physics step. It's the safest way to do it. However, if you set rb.position constantly, you will also get weird behaviour as the physics solver will be running slower than the framerate. You will have your object jumping along every FixedTime interval (which could be say, 0.05 seconds), wheras the game will be rendering at 30 FPS.

 

To avoid this, use rigidbody.MovePosition and rigidbody,MoveRotation. Use them in FixedUpdate so that they will work with the physics solver correctly. 

 

tl:dr:

 

Don't use Transform or any of its methods for physics objects, use Rigidbody methods (specifically, MovePosition, MoveRotation, ApplyForce, ApplyAngularForce, etc) and use them in FixedUpdate


sig.png
I really don't do requests and my Arnor Soldier is not fit for BFME. Don't ask me for either.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users