Search Unity

raycast to pivot not to center object

Discussion in 'Scripting' started by Arcan-Studios, May 30, 2015.

  1. Arcan-Studios

    Arcan-Studios

    Joined:
    Mar 12, 2015
    Posts:
    58
    hi there, i m trying to make raycast from one object to another the raycast goes from one center object to another center and the both object have children so the center point its in a bad location so the raycast doesnt work how i expected i need the raycast to go form pivot point to pivot point, its posible to do that? and how? thanks
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Actually always try to fix your pivot positions using empty gameobjects as a parent, yes there can be some problems about center positions if you import a mesh, so very basic trick to use you can create an empty game object on both of those objects, set their positions to where you want the centers to be, and then over your script assign those empty objects and cast your rays from there, with the direction between them.

    Code (CSharp):
    1. public Transform t_Center1;
    2. public Transform t_Center2;
    3.  
    4.  
    5. void Update()
    6. {
    7. RaycastHit hit;
    8. Vector3 dir = t_Center2.position - t_Center1.position; // Create a vector which is the direction between these to centers.
    9.  
    10. if(Physics.Raycast(t_Center1.position, dir, out hit))
    11. {
    12. // Your code here
    13. }
    14. }
    This casts the ray from the Center1 position, to Center2 position. And you will set those positions on the scene tab according to your needs.
     
  3. Arcan-Studios

    Arcan-Studios

    Joined:
    Mar 12, 2015
    Posts:
    58