Search Unity

Parkour Vault Make Rigidbody Flip

Discussion in 'Scripting' started by KoldGames, Apr 23, 2014.

  1. KoldGames

    KoldGames

    Joined:
    Jan 30, 2014
    Posts:
    6
    Hello! I'm working on a completely procedural parkour system for my game, and after finishing wall climbing and running, I am now to the point where I need to implement vaulting. I tried to implement it, and while it could detect when to vault, my player (rigidbody) would leap into the air and flip over the obstacle but roll around on the ground when I landed, lol. It wasn't supposed to flip, but it gave me a brand new idea for something I would like to add to my game later. First question, how would I make the rigidbody flip 360 degrees (front flip) and complete it's rotation right when it is about to hit the ground? My original approach to this was check if the player wants to vault, and if so, apply torque to the player (causing it to flip), send a ray with a distance of 20 from the player's position and down (0, -1, 0), subtract hit.point's y from player.transform.position.y to get the distance, then if the distance is greater than one (pretty high of ground), add more force to the player to rotate it, and if the distance is less than one (close to the ground), add twice as much force to the player to help finish the rotation and freeze roation when the distance is 0. Although, this didn't always work correctly. Is there a better way to do this? How can I make the player rotate 360 and stop?

    Here is how I check if the player want's to vault:
    Code (csharp):
    1.  if (Camera.main.velocity.x > MinimumVaultSpeed || Camera.main.velocity.x < -MinimumVaultSpeed || Camera.main.velocity.z < -MinimumVaultSpeed || Camera.main.velocity.z > MinimumVaultSpeed)
    2.         {
    3.             RaycastHit hit;
    4.  
    5.             if (Physics.Raycast(new Ray(new Vector3(transform.position.x, transform.position.y - (player.Capsule.height / 3), transform.position.z), new Vector3(Camera.main.transform.forward.x, 0, Camera.main.transform.forward.z)), out hit, 2))
    6.             {
    7.                 Mesh mesh = (hit.transform.GetComponent("MeshFilter") as MeshFilter).mesh;
    8.  
    9.                 Vector3[] vertices = new Vector3[3];
    10.  
    11.                 for (int i = 0; i < 3; ++i)
    12.                 {
    13.                     Vector3 vert = mesh.vertices[mesh.triangles[hit.triangleIndex * 3 + i]];
    14.                     vertices[i] = hit.transform.TransformPoint(vert);
    15.                 }
    16.  
    17.                 float maxY = Mathf.Max(new float[] { vertices[0].y, vertices[1].y, vertices[2].y });
    18.  
    19.                 float heightDiff = maxY - transform.position.y + player.DefaultHeight;
    20.  
    21.                 if (heightDiff > 2)
    22.                 {
    23.                     rigidbody.freezeRotation = false;
    24.                     rigidbody.AddForce(new Vector3(Camera.main.velocity.x, 0, Camera.main.velocity.z) * 25);
    25.                     rigidbody.AddTorque(new Vector3(0, 0, 1) * 100);
    26.                     vaulting = true;
    27.                 }
    28.  
    29.                 return true;
    30.             }
    31.         }
    32.  
    Second question, what is the best way to do vaulting? Thanks! :)
     
  2. Player11132

    Player11132

    Joined:
    Sep 25, 2020
    Posts:
    2
    To flip him you could use AddForceAtPoint and find the perfect force and welp point where to add the force and maybe he will flip , i ain't good at programing either but i hope it helps