Search Unity

Offsetting Movement with a Rigidbody

Discussion in 'Scripting' started by Barachiel, Aug 25, 2015.

  1. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Hi all.

    I'm doing a small mockup of a runner style game as a proof of concept.
    I'm using a custom controller that utilizes a rigidbody, and at the moment the character only needs to run forward and side to side, so that's what it does.
    So I have it running, I have a camera smoothly following it, but I'd like to offset it by a certain amount on the z-axis, controlled by the player, so that there's a bit of give in the character's position for the player to move forward and backward, allowing them to time when they go past obstacles and the like.
    I thought of having a main collider that handles all the normal movement and only collides with guiding geometry, then having an empty gameObject holding the character and related bits that I could offset, letting the character have it's own collider that could react to obstacles.
    This approach obviously wouldn't work, since the timing between the character and it's parent would be off in regards to things like slopes.

    I haven't really had much cause to think about this issue until now, so I'm slightly mystified as to how to go about it. Does anyone have any ideas as to the general direction I should be headed to make this work?

    Thanks for any help and advice.
    Also, my topic title is terrible and vague. Not sure what to put that's short and concise, yet accurate and descriptive. Feel free to suggest a change.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Having a bit of a hard time picturing / understanding your problem.

    Do you mean like the old shoot 'em ups, where the screen scrolls at a certain rate but the ship can move all around the screen?
     
  3. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Aye, something like that.
    The character moves at a fixed rate in a direction I set, unaffected by the player, with the camera following. The only movement they can affect is lateral movement on the local x-axis.
    I want the player to be able to adjust the character's movement in all directions on the ground, within a certain limit (so they can't go off path or too far ahead or back) without touching the character's actual speed or overall movement going forward.

    If you take an old Shmup like R-Type, and pretend it's a top down view, that's kind of close.
    Only, all the complications of a Rigidbody and the requirement to actually move forward.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Any consideration to moving the world rather than moving the player? That simplifies this problem a lot.
     
    Kiwasi likes this.
  5. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    I'd been looking into it, since I'd eventually need to move everything back to origin after a while anyway, but my initial forays into this area have been rather troubling when using a Rigidbody.
    If I can't come up with any other solution then I'll see if I can force it to work, but I'd really rather find a different solution.
    I have a few other ideas to try, but I really have no idea if they'll work or how hard they'll be to implement, so I came here to see if anyone had done this before or if there was something obvious I was missing.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can actually combine Transform.position and Rigidbody movement together. I would set the characters forward movement every frame with transform.position, and then allow the characters local movement to be handled with the ridgidbody.
     
  7. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    One of the things I was considering was using the normal Rigidbody movement as I am now, but allowing the position to be offset with Rigidbody.MovePosition, so that seems similar to your suggestion.
    If I handle the forward movement with Transform.position while using a Rigidbody, don't I run the risk of poor interactions with any other Rigidbody objects? And how would it handle using physics to ascend slopes?
    Or do you propose only setting the z-axis movement, while letting x and y be handled by the Rigidbody?