Search Unity

Question VR Curved RayCast - Sometimes no collision with the ground

Discussion in 'VR' started by Eddi_7, Aug 18, 2021.

  1. Eddi_7

    Eddi_7

    Joined:
    Nov 19, 2018
    Posts:
    4
    Sometimes one of the RayCasts missed the collision or it happens between two RayCasts? Does anybody know how to fix it?

    Issue:


    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CurvedRaycast : MonoBehaviour
    6. {
    7.     //Raycast needs
    8.     public GameObject hitPointObject;
    9.     public LayerMask layerMask;
    10.     public RaycastHit hit;
    11.     public List<Vector3> positions;
    12.     public LineRenderer lineRenderer;
    13.     public Material greenSteps;
    14.     public Material redSteps;
    15.     public bool isTeleporatationsystemActive;
    16.     private Vector3 raycastDirection;
    17.     private Vector3 raycastPosition;
    18.    
    19.     //Settings
    20.     public int rayCastSpawnLimit = 20;
    21.     public float rayCastLength = 0.1f;
    22.     public float gravity = 9.81f;
    23.     private float smooth = 0.01f;
    24.     private int amountOfRaycastsSpawned;
    25.  
    26.     private void FixedUpdate()
    27.     {
    28.         raycastDirection = transform.up * -1.0f;
    29.         curvedRaycast();
    30.         raycastPosition = transform.position;
    31.     }
    32.  
    33.     private void curvedRaycast()
    34.     {
    35.         if (amountOfRaycastsSpawned > rayCastSpawnLimit -1) {amountOfRaycastsSpawned = 0;}
    36.  
    37.         while (amountOfRaycastsSpawned < rayCastSpawnLimit)
    38.         {
    39.             positions.Add(raycastPosition);
    40.             //Raycast in the starting direction and check for a hit
    41.             if (Physics.Raycast(raycastPosition, raycastDirection, out hit, rayCastLength, layerMask))
    42.             {
    43.                 Debug.DrawRay(raycastPosition, raycastDirection * rayCastLength, Color.red, rayCastLength);
    44.                 if(hit.collider.gameObject.tag == "Ground")
    45.                 {
    46.                     isTeleporatationsystemActive = true;
    47.                     GetComponent<Renderer>().material = greenSteps;
    48.                     hitPointObject.SetActive(true);
    49.                 }
    50.                 else
    51.                 {
    52.                     isTeleporatationsystemActive = false;
    53.                     GetComponent<Renderer>().material = redSteps;
    54.                     hitPointObject.SetActive(false);
    55.                 }
    56.                 hitPointObject.transform.position = hit.point;
    57.             }
    58.             else
    59.             {
    60.                 isTeleporatationsystemActive = false;
    61.                 GetComponent<Renderer>().material = redSteps;
    62.                 hitPointObject.SetActive(false);
    63.                 Debug.DrawRay(raycastPosition, raycastDirection * rayCastLength, new Color(Random.Range(0f, 1f),Random.Range(0f, 1f),Random.Range(0f, 1f)), rayCastLength);
    64.  
    65.                 //Adds a new ray cast
    66.                 raycastPosition += raycastDirection * rayCastLength;
    67.                 raycastDirection += new Vector3(0.0f, -gravity * smooth, 0.0f);
    68.                 hitPointObject.transform.position = raycastPosition;
    69.             }
    70.             amountOfRaycastsSpawned++;
    71.         }
    72.  
    73.         //Line renderer
    74.         lineRenderer.startWidth = 0.1f;
    75.         lineRenderer.endWidth = 0.1f;
    76.         lineRenderer.useWorldSpace = true;
    77.         lineRenderer.positionCount = positions.Count;
    78.         lineRenderer.SetPositions(positions.ToArray());
    79.         positions.Clear();
    80.     }
    81. }