Search Unity

Rotating a player when there isn't enough room to rotate

Discussion in 'Scripting' started by Armetron, Sep 1, 2018.

  1. Armetron


    Oct 7, 2016
    Hello again community

    Today I've been working on getting the player to rotate and point his/her "feet" in the direction that I want (gravityDirection) in my testing I've used
    Code (CSharp):
    1. void FixedUpdate() {
    2.      transform.rotation = Quaternion.FromToRotation(Vector3.down, gravityDirection);
    3. }
    It works despite Snapping the player instantly (something I'll work on latter) however the problem I'm faced with is when the player is standing between two surfaces, examples could be two walls, a door frame, ext..., and I try to rotate into an orientation that he/she would not fit one of two things could happen

    1. The Physics engine collider will spit the player out into a space that they would fit.
    2. The player would clip into an object.

    What I want to happen is the player will try to rotate but if something is in the way its stops until the player moves into a bigger space. I plan to give some control for the player to get themselves unstuck


    Whats happening, gravityDirection is set to go ------> to the right

    What I would like, trying to rotate but still being prevented

    any help and advice will be apreciated

    the context of the game is a shooter that players have gravity manipulation gear to reorient themselves and enemies, this problem came to mind when I considered what would happen if I stood in a doorway and had myself fall sideways banging my head in the process
  2. Doug_B


    Jun 4, 2017
    You could just use forces rather than manually controlling the movement and rotations. That leaves things like this (both moving smoothly and responding appropriately to collisions) to the physics engine to resolve.

    Alternatively, when you manually lerp (or similar) the rotation, you will have to check for a collision and write the code as to how to respond to that collision event yourself.