Search Unity

Raycast Not Triggered On Certain Vectors

Discussion in 'Getting Started' started by ligeti32, Jul 14, 2020.

  1. ligeti32

    ligeti32

    Joined:
    Jul 10, 2020
    Posts:
    3
    Apologies if this is the wrong forum. I'm a teacher who typically works with sound and audio but I'm attempting to build a simple flight-simulation environment to facilitate some remote learning exercises.

    My issue is that a raycast will occasionally not be triggered due to the orientation of the plane. I am wondering if it is because it's rotation is being reduced to zero when it is pointing in a number of directions. The rotation and positioning of the ray's beginning and end is perfectly fine and it is reacting as expected when it reaches a target. Throttle speed has no effect on the issue.

    This isn't my typical watering hole so any advice would be appreciated!


    Code (CSharp):
    1.  
    2. void Update()
    3.     {
    4.         if (Input.GetButton("Fire1"))
    5.         {
    6.             if (Time.time > m_shootRateTimeStamp)
    7.             {
    8.                 shootRay();
    9.                 m_shootRateTimeStamp = Time.time + shootRate;
    10.             }
    11.         }
    12.     }
    13.     void shootRay()
    14.     {
    15.         Ray ray = new Ray (transform.position, transform.forward);
    16.         RaycastHit hit;
    17.  
    18.         if (Physics.Raycast(ray, out hit, range))
    19.         {
    20.             Debug.Log(hit.transform.name);
    21.  
    22.             Target target = hit.transform.GetComponent<Target>();
    23.             if (target != null)
    24.             {
    25.                 target.TakeDamage(damage);
    26.             }
    27.  
    28.             GameObject laser =
    29.                 GameObject.Instantiate(m_shotPrefab, transform.position, transform.rotation) as GameObject;
    30.             laser.GetComponent<ShotBehavior>().setTarget(hit.point);
    31.             GameObject.Destroy(laser, 2f);
    32.  
    33.            
    34.         }
     
  2. ligeti32

    ligeti32

    Joined:
    Jul 10, 2020
    Posts:
    3
    Upon further inspection, it seems that the Raycast event won't be triggered if the vector is aimed at a space where there will be no terrain or object hits, so I will see if there is a fix for that.
     
  3. ligeti32

    ligeti32

    Joined:
    Jul 10, 2020
    Posts:
    3
    By using an else statement, I have allowed the rays to be cast at an imaginary target at the end of its range if it doesn't encounter any colliding objects when Fire1 is engaged. The target position is correct but rotation is off. I will take a look at the governing shot behavior for the prefab but this is now what the shootRay event looks like:

    Code (CSharp):
    1.  
    2.  
    3. void shootRay()
    4.     {
    5.         Ray ray = new Ray (transform.position, transform.forward);
    6.         RaycastHit hit;
    7.  
    8.         if (Physics.Raycast(ray, out hit, range))
    9.         {
    10.             Debug.Log(hit.transform.name);
    11.  
    12.             Target target = hit.transform.GetComponent<Target>();
    13.             if (target != null)
    14.             {
    15.                 target.TakeDamage(damage);
    16.             }
    17.  
    18.             GameObject laser =
    19.                 GameObject.Instantiate(m_shotPrefab, transform.position, transform.rotation) as GameObject;
    20.             laser.GetComponent<ShotBehavior>().setTarget(hit.point);
    21.             GameObject.Destroy(laser, 2f);
    22.         }
    23.         else
    24.         {
    25.             // If we did not hit anything, shoot a laser at the skybox.
    26.            
    27.            
    28.             GameObject laser =
    29.                 GameObject.Instantiate(m_shotPrefab, transform.position, transform.rotation) as GameObject;
    30.             laser.GetComponent<ShotBehavior>().setTarget(transform.forward * range);
    31.         }
    32.     }
    33.  
    34.