Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Rotating based on floor

Discussion in 'Physics' started by Cosmology27, Apr 28, 2021.

  1. Cosmology27


    Jul 11, 2019
    So I'm trying to make a skater type game like Tony Hawk's Pro Skater. I know I'm going to run into tons of problems, because the logic/physics is already confusing me, but I like those kinds of challenges.

    My current problem I believe is just my ignorance about how physics works. I want the character to change Y rotation ONLY by user input, and to change X rotation based on the ground under the skateboard (Z rotation should never change). In terms of physics, the character is currently a capsule rigidbody, which I think makes the most sense to be able to interact with physics, and be smooth (I'll just make it invisible, and have a character mesh over it eventually). At this point I have made the capsule on it's side forwards lengthwise, and I have locked the Y and Z rotations (so it should only rotate forwards and backwards, to interact with ramps). This way, it cannot tip over, and it will ride the ramps properly.

    Problem 1:
    My problem now is that the thing feels entirely uncontrollable. Any adding of force makes it rock around in a weird way. When I try to go forward it rocks and and forth like a teeter totter (although it does move forwards quite well).

    Problem 2:
    Rotation locks seem to be local or something, because I can quite easily mess up the character and it starts flipping out all crazy. I can watch the rotation in the inspector, and first of all, the Y and Z rotations are changing, when its locked, which is annoying. Also, the X rotation is the one that's freaking out, making it roll all over like a sausage, but it shouldn't be. It looks like the global X rotation is locked (maybe?) but because I've moved, the local X rotation is different, so now it can roll on its side? It seems as though any chance I give it to rotate, it will do something weird. Like if I place it on a ramp and leave it there, it will start rolling to the side, which is shouldn't do, because the rotations are locked. It should only be able to rotate forwards and backwards right now, but for some reason it's rotating on the Z axis.
    How do I make the rotation locks actually lock properly? How do I lock global VS local rotations, and basically just make it so it will ONLY ever rotate forward and backward, locally.

    Problem 3:
    How do I change the forward force that I have to constantly be forward? So if I have a force of 20, and I rotate my character 45 degrees to the left, I now want the 20 force to be going in that direction. Hopefully that makes sense. Basically how the Tony Hawk game would be, where if you're going fast and you hold left, your character turns to the left and continues their momentum.

    Problem 4:
    The rotating backwards and forwards feels bad too. Sometimes I slide up the ramp on my nose. The force just feels weird. If it's a quarter pipe, I basically go up it without tilting, when I should be tilting. I'm guessing it's because the force is forwards on the Z axis, rather than forwards relative to where my face is pointing.

    This one makes the force go forwards on the Z wherever I'm pointing. (zMove is just pressing W or up on controller).
    Vector3 V3Force = CharSpeed * transform.up * zMove;

    I tried this one, which "kinda" makes the force go forwards where my nose is pointing
    rb.AddRelativeForce(new Vector3(0, zMove, 0) * CharSpeed);

    Problem 5:
    Even just if I have a lot of speed going towards a quarter pipe, the character won't even reach the top. It's not tall, only like 6m tall or something, and the force just plummets the moment the character starts riding it. It goes up a little, but the force just dies. This isn't really how things interact in physics, so I'm wondering how I can make it actually ride the ramp like things do in real life.
    Last edited: Apr 29, 2021
  2. AlTheSlacker


    Jun 12, 2017
    1. I'll guess that you have ground friction and you are applying the force at the rb centre of gravity. Imagine you are trying to push a chair across a carpeted floor - if you push high up on the chair it will lean over as the feet are not sliding. On all your colliders you will have a physics material (if not they will use the unusually configured default). Make yourself a new physics material with very low (say 0.05, but have an experiment) friction and zero bounce. Apply this material to all your colliders. (read this for more info on physics materials This is going to give you plenty of other problems, not least will be your next question which will no doubt be "how do I stop my skateboard sliding sideways".....*

    2. Unfortunately partial constraints on rb's do not work robustly. You can improve this by changing your physics solver type via: Edit - Project Settings - Physics - Solver Type - Temporal Gauss Seidel. If this does not work you may want to consider using a PID controller logic to stabilise the player, this has the advantage of being a little softer than a rigid constraint. Here is some info to get you started on that :

    3. Use your player's transform.forward vector to set the direction of the force.

    4. Although I am not very clear on what you are asking here, you have to be aware that this is a simple rb physics solver, whereas a human will be generating all sorts of forces to control their balance / CoG position. If you are trying to produce your board riding characteristics from just physics, this will be extremely difficult.

    5. You say the force just dies, but what force? It will all depend on how you are representing the wheels of the skateboard.

    *The problem that you are going to really struggle with is the one I hinted at in part 1. Your actual contact with the ground is via some wheels that have almost no drag in one direction and plenty transversely. You will either have to write your own physics model of this or pursued some wheel colliders to do what you want. If you are doing this for fun, I suggest you pick some simpler starting projects, perhaps just look at getting a board to roll down a slope and up the other side without a rider or direction control, then try and increase complexity slowly, expect to spend a long time getting anywhere.

    Did you have a look around at what other people have done? There is quite a bit on hover boards and this might be worth checking out (not looked at it myself.....)

    Good luck.
    Cosmology27 likes this.