Search Unity

Skatepark physics

Discussion in 'Physics' started by Mehd, Mar 20, 2017.

  1. Mehd

    Mehd

    Joined:
    Apr 29, 2016
    Posts:
    91
    Hi everyone,

    I'm trying to get some skatepark physics working ! I've disabled Gravity for rigidbodies and I wrote this script to ensure that the character is able to follow curves and so on.

    Code (CSharp):
    1. public LayerMask modules;
    2.     EtatTest e; // this is the state, records whether or no the character touches the ground
    3.     Rigidbody rb;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.  
    8.         rb = GetComponent<Rigidbody>();
    9.         e = GetComponent <EtatTest>();
    10.        
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.  
    16.         rb.AddForce(-10*rb.mass*Vector3.up);
    17.  
    18.         if(!e.OnGround)
    19.         {
    20.             Ray ray = new Ray (transform.position, -transform.up);
    21.             RaycastHit hit;
    22.  
    23.            
    24.             if(Physics.Raycast(ray, out hit))
    25.             {
    26.                 Quaternion Rot = transform.rotation*Quaternion.FromToRotation(transform.up, hit.normal);
    27.                 transform.rotation = Quaternion.Lerp(transform.rotation, Rot, 5*Time.deltaTime);
    28.             }
    29.         }
    30.         else
    31.         {
    32.             Ray ray = new Ray (transform.position, -transform.up);
    33.             RaycastHit hit;
    34.  
    35.            
    36.  
    37.             if(Physics.Raycast(ray, out hit, modules))
    38.             {
    39.                 Quaternion Rot = transform.rotation*Quaternion.FromToRotation(transform.up, hit.normal);
    40.                 transform.rotation = Quaternion.Lerp(transform.rotation, Rot, 5*Time.deltaTime);
    41.             }
    42.             else if(Physics.Raycast(ray, out hit))
    43.             {
    44.                 Quaternion Rot = transform.rotation*Quaternion.FromToRotation(transform.up, Vector3.up);
    45.                 transform.rotation = Quaternion.Lerp(transform.rotation, Rot, 5*Time.deltaTime);
    46.             }
    47.         }
    48.        
    49.     }

    Skatepark.png


    So far, the results aren't good. Any ideas ?

    Thanks !
     
  2. Mehd

    Mehd

    Joined:
    Apr 29, 2016
    Posts:
    91
    Anyone ? Anything ?