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

Finding the Correct Angle for Multiple Projectiles

Discussion in 'Scripting' started by DevoMage, Aug 29, 2014.

  1. DevoMage

    DevoMage

    Joined:
    Dec 19, 2012
    Posts:
    16
    my current code produces this:

    raycast.PNG

    when the 3 projectiles are fired, they converge to the point where the crosshairs are pointed.

    converge.PNG

    the desired result is for them to maintain their arrangement.

    desired_result.PNG

    of course, this is quite easy for one projectile... not so easy for multiple projectiles.

    thanks in advance for any direction on this.


    Code (csharp):
    1. Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    2.  
    3. RaycastHit hit;
    4.  
    5. bool bHit = Physics.Raycast(ray, out hit);
    6.  
    7. //create 3 projectiles in the form of a triangle - one on top and two below
    8. //  0
    9. // 1 2
    10.  
    11. //rotation
    12. p0.transform.rotation = Quaternion.LookRotation(hit.point - p0.transform.position);
    13. p1.transform.rotation = Quaternion.LookRotation(hit.point - p1.transform.position);
    14. p2.transform.rotation = Quaternion.LookRotation(hit.point - p2.transform.position);
    15.  
    16. //projectile final impact must be where the crosshairs are pointing.  an offset of the point of reference ray
    17. Debug.DrawRay(p0.transform.position, p0.transform.forward * 50f, Color.green, 10f);
    18. Debug.DrawRay(p1.transform.position, p1.transform.forward * 50f, Color.red, 10f);
    19. Debug.DrawRay(p2.transform.position, p2.transform.forward * 50f, Color.blue, 10f);
     
    Last edited: Aug 29, 2014