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

Ray position not updating to gameObject

Discussion in 'Scripting' started by kai_226, Nov 2, 2017.

  1. kai_226

    kai_226

    Joined:
    Jul 6, 2017
    Posts:
    53
    Hey guys, I am shooting a Ray down from an object's position. The object moves and the Ray goes with the object, all fine.
    However, I need to offset the Ray a little bit. For example, I want to offset it a bit down and forward to the current position of it's gameObject. With the code below, the Ray does stay infront of the object [in world space] even though I rotate the object around.

    Code (CSharp):
    1. Debug.DrawRay(new Vector3(transform.position.x - 1.5f,
    2.     transform.position.y + .15f, transform.position.z), -transform.up);
    How can I align the Ray to the position and rotation of the gameObject that uses the Ray?

    Thx!
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You can use the relative forward and down to calculate the offset.

    Code (csharp):
    1.  
    2. var pos = transform.position + transform.forward * -1.5f + transform.down * 0.15f;
    3.  
     
    kai_226 likes this.
  3. kai_226

    kai_226

    Joined:
    Jul 6, 2017
    Posts:
    53
    Awesome, that works!
    Thanks a bunch :)
     
    KelsoMRK likes this.