Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Shoot a raycast toward a direction inside a givin radius

Discussion in 'Scripting' started by thierry258, Aug 20, 2019.

  1. thierry258

    thierry258

    Joined:
    Jan 3, 2017
    Posts:
    8
    Hi,

    Here's my problem, I try to shoot a Raycast that start from a random point of a sprite. Sound weird, if you check the GIF, meaby you will understand more easily. ;)

    Aiming_System.gif

    There's a heartbeat system that check the heartbeat of the player and adjust the aiming circle on the screen (normal sprite of 200x200) with it. If the heartbeat is higher, the aiming circle update more quickly and get bigger. The Gun script need to shoot a raycast from a random position inside this circle. I want to know if there's a way to get a point in the world (vector3) base on a UI element (the aiming circle). For now, I use a value find by trying, but if the precision of the gun change, this value doesn't work anymore, so I really want a way that will be polyvalent.

    Thanks in advance for your help :)
     
  2. IcePhoenix_0101

    IcePhoenix_0101

    Joined:
    Sep 9, 2017
    Posts:
    32
    I don't think the position of UI element matter that much in your case. If the UI remains in the center, you can give it an offset according to the Heartbeat value.

    Code (CSharp):
    1. Vector2 offset = Random.insideUnitCircle * 0.05f * x;
    2. Vector3 rayOrigin = Camera.main.ViewportToWorldPoint(new Vector3(0.5f + offset.x, 0.5f + offset.y, 0.0f));
    x being the heartbeat value scaled to the range between 0 and 1.

    You can change the 0.05 value, to change the maximum offset radius.
     
    thierry258 and gononono64 like this.
  3. thierry258

    thierry258

    Joined:
    Jan 3, 2017
    Posts:
    8
    I have try what you said, but it doesn't seems to work? The return value only give the aCam position, there's no offset done, even if the offset is different every time...

    Code (CSharp):
    1. ///<summary> Return a random point from the aiming system in the world </summary>
    2.     private Vector3 GetRandomStartPoint(Camera aCam, float aAimingSize)
    3.     {
    4.         //Get the random position
    5.         Vector2 offset = Random.insideUnitCircle * 0.05f * aAimingSize;
    6.        
    7.         //Set the new random position
    8.         m_RandomStartPoint3D.x = 0.5f + offset.x;
    9.         m_RandomStartPoint3D.y = 0.5f + offset.y;
    10.         m_RandomStartPoint3D.z = 0.0f;
    11.  
    12.         Debug.Log("Random Circle Value: " + m_RandomStartPoint3D);
    13.  
    14.         return aCam.ViewportToWorldPoint(m_RandomStartPoint3D);
    15.     }
     
  4. thierry258

    thierry258

    Joined:
    Jan 3, 2017
    Posts:
    8
    I find a solution with your help, thanks a lot!

    Code (CSharp):
    1. ///<summary> Shoot a raycast to check the destination distance, and then shoot an other raycast to check ennemi </summary>
    2.     private void ShootRaycast(Camera aCam, float aAimingSize)
    3.     {
    4.  
    5.         m_RayOrigin = aCam.transform.position;
    6.  
    7.         if (Physics.Raycast(m_RayOrigin, aCam.transform.forward, out m_ShootTarget, 100f))
    8.             {
    9.                 SetRayDestination(aCam, aAimingSize, m_ShootTarget.distance);
    10.                 m_RayDirection = m_RayDestination - m_RayOrigin;
    11.  
    12.                 if (Physics.Raycast(m_RayOrigin, m_RayDirection, out m_ShootHit, 100f))
    13.                 {
    14.                     if (m_ShootHit.transform.GetComponent<EnnemiTest>() != null)
    15.                     {
    16.                         m_ShootHit.transform.GetComponent<EnnemiTest>().GetDamage(m_GunStats.dammageByBullet);
    17.                         Debug.Log("Ennemi Touch Raycast");
    18.                     }
    19.                 }
    20.             }
    21.     }
    Code (CSharp):
    1. ///<summary> Set a random point from the aiming system in the world </summary>
    2.     private void SetRayDestination(Camera aCam, float aAimingSize, float aDistance)
    3.     {
    4.         //Get the random position with the distance a hearbeat scale value
    5.         m_Offset = Random.insideUnitCircle * 0.02f * aDistance * aAimingSize;
    6.         m_RayDestination = aCam.transform.position;
    7.  
    8.         //Set the new random destination with the offset
    9.         m_RayDestination += aCam.transform.forward * aDistance;
    10.         m_RayDestination += aCam.transform.right * m_Offset.x;
    11.         m_RayDestination += aCam.transform.up * m_Offset.y;
    12.  
    13.     }
    ezgif-5-53bd013a211f.gif