Search Unity

Best way to align ridgidbody skateboard with floor?

Discussion in 'Scripting' started by Parappa, Oct 11, 2013.

  1. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    $Skate.jpg

    I want the board to align to the floor (ramps/uneven surface) without the whole thing falling over. I dont want the standard raycast down from the middle > get floor angle> set whole thing to floor angle, because that doesn't look right in certain situations and doesn't give the phyical feel.

    Some ideas I had:
    Limiting how much the ridged body can rotate somehow
    Setting a toque to the upright position
    Human always faces up and pushes down on the board
     
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    I don't have any experience with at, so slap me and call my Valentine, but... couldn't you just use WheelColliders for the wheels and an other collider for the board and let the physic engine handle everything else? With a handy script you can also control the wanted alignment of the rigidbody for the whole construct. As the player has weight/mass he will also push down the board on his own.
     
  3. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    "couldn't you just use WheelColliders for the wheels and an other collider for the board and let the physic engine handle everything else?"
    Basically what I'm doing now

    "control the wanted alignment of the rigidbody"
    Yeah that's my first idea but I'm not really sure how to go about doing it. I could do something like 'if x angle > 45, x angle = 45'. But that would probably be quite jankey with a ridgedbody
     
  4. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21
    You could use a Lerp or Slerp to smooth the raycast hit normal giving it a more natural feel.

    Code (csharp):
    1.  
    2. float rotSmooth = 5.0f;
    3. RaycastHit hit;
    4. if (Physics.Raycast(transform.position, -transform.up, out hit))
    5. {
    6.     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hit.normal), hit.normal), Time.deltaTime * rotSmooth);
    7. }
    8.  
    Increase rotSmooth for a quicker transition, decrease it for a slower transition.

    Even better would be to raycast from each wheel and average the normal hits to smooth transitions between surfaces even further.
     
    Last edited: Oct 11, 2013
  5. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    I'll try that last idea
    Since the wheels are raycasting a already is there anyway to get the normal from their hit?

    That would still make it float in the air if the back wheel where on a surface higher than the front wheels
     
  6. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21
    There are several ways to do this.. I suppose you could create a public array of Vector3 for the normals and use OnCollision in a script on each wheel to set them sending a message from the wheel script to the script that does the rotation smoothing.
     
unityunity