Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

(Solved) Relative position from GameObject.

Discussion in 'Scripting' started by CFK, Jan 19, 2018.

  1. CFK

    CFK

    Joined:
    Dec 13, 2014
    Posts:
    12
    Want to create ray-casts offset from the game object.
    I guessing my code is offset from the world coordinate.

    My game play offset problem.
    https://imgur.com/tbIcLbr

    How to offset it from local coordination? or other way to achieve it?



    Code (CSharp):
    1.         Vector3 rayPos = new Vector3(transform.position.x, 0, transform.position.z+2);
    2.         Vector3 rayPos3 = new Vector3(transform.position.x+2, 0, transform.position.z + 2);
    3.         Vector3 rayPos4 = new Vector3(transform.position.x-2, 0, transform.position.z + 2);
    4.         Vector3 rayPosBack = new Vector3(transform.position.x, 0, transform.position.z - 2);
    5.  
    6.         Debug.DrawRay(rayPos, transform.forward * 200, Color.red);
    7.         Debug.DrawRay(rayPos, transform.right * 200, Color.blue);
    8.         Debug.DrawRay(rayPos, transform.right * -200, Color.green);
    9.         Debug.DrawRay(rayPos3, transform.forward * 200, Color.yellow);
    10.         Debug.DrawRay(rayPos4, transform.forward * 200, Color.cyan);
    11.         Debug.DrawRay(rayPosBack, transform.right * 200, Color.black);
    12.         Debug.DrawRay(rayPosBack, transform.right * -200, Color.white);
    13.  
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You do this with Transform.TransformPoint.

    If this is a super-common thing and you want to be able to freely move the origin positions of the raycasts, you might think about making new GameObjects as children of the primary one, offsetting them however you like locally, and just using those Transforms as the various origin points directly using public Transform references and drag-and-dropping them into the inspector.
     
  3. CFK

    CFK

    Joined:
    Dec 13, 2014
    Posts:
    12
    Thank you, Lysander.

    Transform.TransformPoint. works perfectly.
    Code (CSharp):
    1.         Vector3 rayPos = transform.TransformPoint(Vector3.forward * 2);
    2.         Vector3 rayPos3 = transform.TransformPoint(Vector3.right * 2);
    3.         Vector3 rayPos4 = transform.TransformPoint(Vector3.right * -2);
    4.         Vector3 rayPosBack = transform.TransformPoint(Vector3.forward * -2);
    5.  
    6.  
    7.  
    8.         Debug.DrawRay(rayPos, transform.forward * 200, Color.red);
    9.         Debug.DrawRay(rayPos, transform.right * 200, Color.blue);
    10.         Debug.DrawRay(rayPos, transform.right * -200, Color.green);
    11.         Debug.DrawRay(rayPos3, transform.forward * 200, Color.yellow);
    12.         Debug.DrawRay(rayPos4, transform.forward * 200, Color.cyan);
    13.         Debug.DrawRay(rayPosBack, transform.right * 200, Color.black);
    14.         Debug.DrawRay(rayPosBack, transform.right * -200, Color.white);
    15.  
    16.        
    17.