Search Unity

Raycast HIT Offset

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

  1. DevoMage

    DevoMage

    Joined:
    Dec 19, 2012
    Posts:
    16
    I'm trying to get the correct rotation for each projectile when there are multiple projectiles (at different start positions) per cast. for example, two fireballs side by side. the origin of each projectile has an elevation and cast distance based on the player's position.

    attached is a complete demo script.

    raycast_hit_offset.PNG

    this image shows the raycast based on different player rotation.

    i believe the problem is correctly adding the offset to the original ray hit location.

    any suggestions would be helpful. thanks in advance!



    Code (CSharp):
    1.  
    2.  
    3. //get a raycast hit as a point of reference where the projectiles should end up
    4. Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    5.  
    6. RaycastHit hit;
    7.  
    8. bool bHit = Physics.Raycast(ray, out hit);
    9.  
    10.  
    11. ...
    12.  
    13.  
    14. //projectile final impact must be where the crosshairs are pointing.  an offset of the point of reference ray
    15. Debug.DrawLine(p1.transform.position, hit.point - new Vector3(spacing, 0, 0), Color.red, 15);//left of the reference ray
    16. Debug.DrawLine(p2.transform.position, hit.point + new Vector3(spacing, 0, 0), Color.blue, 15);//right of the reference ray
    17.  
    18. //rotation
    19. p1.transform.rotation = Quaternion.LookRotation(hit.point - new Vector3(spacing, 0, 0) - p1.transform.position);
    20. p2.transform.rotation = Quaternion.LookRotation(hit.point + new Vector3(spacing, 0, 0) - p2.transform.position);
    21.  
    22.  
    23.  
     

    Attached Files: