Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Consistent Rigidbody Motion

Discussion in 'Scripting' started by camerontownsend, Jan 26, 2021.

  1. camerontownsend

    camerontownsend

    Joined:
    Mar 7, 2019
    Posts:
    14
    Hello superior coders,
    I’m running into an issue that I’m sure most of you solved long ago, but I'm still a noob so I could use some help.

    I’m making a simple Binding-Of-Isaac-style hack-and-slash system with a rigidbody character and I'm trying to get the player to move at a consistent distance and speed across all platforms and frame rates. I do not want gradual acceleration; I need "move > stop" like Pac-Man but with rigidbody physics. Also, the Rigidbody needs to move in the direction of a quaternion called "LookAt.joystickDirection" without physically rotating.

    Currently I'm using AddForce and clamping the magnitude but that's giving me inconsistent results across platforms- especially on iPhone. I’m sure there’s some simple, universally-accepted way to move a rigid body around consistently across platforms that I just haven't heard of yet.

    Any advice would be seriously appreciated. And if I’m being too vague please let me know- I’m still new to this. Thanks in advance!

    Note:
    Also I'm pretty sure a quaternion does two 180's as opposed to a 360, thus when the joystick goes to what would be 180, it flashes to 0 for a second, causing the player to flash right in the middle of its leftward motions. It would be nice if there was a workaround for that. Probably some simple .something I'm just not adding.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You can just set the velocity of your rigidbody directly.
    rb.velocity = <whatever velocity you want>
    . If you're getting inconsistent results with AddForce though, I'd guess you're probably calling AddForce within Update(). That will give you inconsistent results depending on the framerate.

    As for the quaternion question, I'm guessing you're using the normalized joystick input direction? That can get funky when the joystick input is very small. One option is to use a bigger deadzone.
     
  3. camerontownsend

    camerontownsend

    Joined:
    Mar 7, 2019
    Posts:
    14
    You again lol
    Thanks for your help. I didn't know rb.velocity was a steady, consistent speed- I thought it was like... idk. However I am curious- you said AddForce is inconsistent in Update()- where are you supposed to use it effectively? FixedUpdate()?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Yep, it's a constant speed.
    Exactly - you do it in FixedUpdate.
     
  5. camerontownsend

    camerontownsend

    Joined:
    Mar 7, 2019
    Posts:
    14
    Your wisdom and teachings are greatly appreciated.