Search Unity

Ground hugging vehicles glitch and jumping issue.

Discussion in 'Getting Started' started by ahsan9898, Feb 19, 2020.

?

Ground hugging vehicles glitch and jumping issue.

  1. The glitch might be due to surface smoothness but in my case my track is very smooth.

    1 vote(s)
    50.0%
  2. May be trying to disable the ray casting while at jump state and enable physics work.

    2 vote(s)
    100.0%
Multiple votes are allowed.
  1. ahsan9898

    ahsan9898

    Joined:
    Oct 8, 2019
    Posts:
    6
    I'm working on a 3d Skateboard game in unity and wants my skateboard to hug the ground surface, which is like a round pipe and my skateboard is moving inside that pipe and at some points that pipe is open from sides to view outside world. My skateboard hugs the pipe mesh perfectly but when my skateboard reaches at the open edges of track(pipe) it starts glitching. I'm doing ground hugging via ray casting and getting normals of the surface and aligning the skateboard rotation according to the surface normals via Quaternion.Lerp() function. Also i want my skateboard to jump from the ramps on its way. Does anyone has an idea how to achieve that?
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    u can addforce to jump from ramp
     
  3. ahsan9898

    ahsan9898

    Joined:
    Oct 8, 2019
    Posts:
    6
    thanks for replying, the major issue is the glitch at edges, do you have any idea of how to fix this? Sorry i can't provide you the example code for now, i'll do so as soon as possible.
     
  4. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    maybe post screenshot and how raycasts are emenating from skateboard later?
     
  5. ahsan9898

    ahsan9898

    Joined:
    Oct 8, 2019
    Posts:
    6
    Here's the code. I'm calculating normal of the surface and adjusting my skateboard's rotation accordingly.
     
  6. ahsan9898

    ahsan9898

    Joined:
    Oct 8, 2019
    Posts:
    6
    Code (CSharp):
    1. public class MoveAlongSurface : MonoBehaviour
    2. {
    3.     [Header("Ship handling")]
    4.     public float fwdAccel = 100f;
    5.     public float fwdMaxSpeed = 200f;
    6.     public float brakeSpeed = 200f;
    7.     public float turnSpeed = 50f;
    8.     private float currentSpeed;
    9.     [Header("Hovering")]
    10.     public float hoverHeight = 3f;        //Distance from the ground
    11.     public float heightSmoothing = 10f;   //How fast the ship will readjust to "hoverHeight"
    12.     public float normalRotSmoothing = 5f; //How fast the ship will adjust its rotation to match track normal
    13.     public LayerMask groundLayer;
    14.     public float yaw;
    15.     private Vector3 previousUpDir;
    16.     private float smoothY;
    17.     // Attach this script to a camera and it will
    18.     // draw a debug line pointing outward from the normal
    19.     void Update() {
    20.         //user inputs
    21.         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {
    22.             currentSpeed += (currentSpeed >= fwdMaxSpeed) ? 0f : fwdAccel * Time.deltaTime;
    23.         } else {
    24.             if (currentSpeed > 0) {
    25.                 //decrease if no button pressed
    26.                 currentSpeed -= brakeSpeed * Time.deltaTime;
    27.             } else {
    28.                 currentSpeed = 0f;
    29.             }
    30.         }
    31.         yaw += turnSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
    32.         //current up dir
    33.         previousUpDir = transform.up;
    34.         //left and right rotation
    35.         transform.rotation = Quaternion.Euler(0, yaw, 0);
    36.         // Only if we hit something, do we continue
    37.         RaycastHit hit;
    38.         if (Physics.Raycast(transform.position, -previousUpDir, out hit, groundLayer)) {
    39.             Debug.DrawLine(transform.position, hit.point, Color.green);
    40.          
    41.             smoothY = Mathf.Lerp(smoothY, hoverHeight - hit.distance, Time.deltaTime * heightSmoothing);
    42.             transform.localPosition += previousUpDir * smoothY;
    43.         }
    44.         /*Moving along surface normal*/
    45.         if (!Physics.Raycast(transform.position, -previousUpDir, out hit)) {
    46.             return;
    47.         }
    48.      
    49.         // Just in case, also make sure the collider also has a renderer
    50.         // material and texture
    51.         MeshCollider meshCollider = hit.collider as MeshCollider;
    52.         if (meshCollider == null || meshCollider.sharedMesh == null) {
    53.             return;
    54.         }
    55.         Mesh mesh = meshCollider.sharedMesh;
    56.         Vector3[] normals = mesh.normals;
    57.         int[] triangles = mesh.triangles;
    58.         // Extract local space normals of the triangle we hit
    59.         Vector3 n0 = normals[triangles[hit.triangleIndex * 3 + 0]];
    60.         Vector3 n1 = normals[triangles[hit.triangleIndex * 3 + 1]];
    61.         Vector3 n2 = normals[triangles[hit.triangleIndex * 3 + 2]];
    62.         // interpolate using the barycentric coordinate of the hitpoint
    63.         Vector3 baryCenter = hit.barycentricCoordinate;
    64.         // Use barycentric coordinate to interpolate normal
    65.         Vector3 interpolatedNormal = n0 * baryCenter.x + n1 * baryCenter.y + n2 * baryCenter.z;
    66.         // normalize the interpolated normal
    67.         interpolatedNormal = interpolatedNormal.normalized;
    68.         // Transform local space normals to world space
    69.         Transform hitTransform = hit.collider.transform;
    70.         interpolatedNormal = hitTransform.TransformDirection(interpolatedNormal);
    71.         // Display with Debug.DrawLine
    72.         Debug.DrawRay(hit.point, interpolatedNormal);
    73.         Quaternion tilt = Quaternion.FromToRotation(transform.up, interpolatedNormal) * transform.rotation;
    74.         //apply rotation
    75.         transform.rotation = Quaternion.Lerp(transform.rotation, tilt, normalRotSmoothing);
    76.         //move ship
    77.         transform.position += transform.forward * (currentSpeed * Time.deltaTime);
    78.     }
    79. }