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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Debug Ray Strange Result?

Discussion in 'Scripting' started by taskodinson, Aug 22, 2013.

  1. taskodinson

    taskodinson

    Joined:
    Jul 16, 2013
    Posts:
    23
    Hi, I'm following a tutorial series and I'm trying to create a debug ray from my character to my camera but for some reason its wrong. I have a transform child on my character that I am getting, and then I set the camera a distance away and up using this code where targetPosition will be the camera and follow.position is the character.

    Code (csharp):
    1. targetPosition = follow.position + follow.up * distanceUp - follow.forward * distanceAway;
    2. Debug.DrawRay(follow.position, follow.up * distanceUp, Color.red);
    3. Debug.DrawRay(follow.position, -1f* follow.forward * distanceAway, Color.blue);
    4. Debug.DrawRay(follow.position, targetPosition, Color.green);
    The strange thing is that the camera works as expected and follows the character, and the red and blue rays also work fine. The green ray however ends up just being a line from my character to some random point in the negative z axis miles away with no y axis at all. I've checked both the values at runtime and they are 5 apart in the z axis but appear to be more like 200. Even when I try to make a line from follow.position to follow.position I still get the same line. Any ideas whats going on?
     
  2. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
  3. taskodinson

    taskodinson

    Joined:
    Jul 16, 2013
    Posts:
    23
    Ahh thank you so much! I should have used draw line.
     
  4. Jallen182

    Jallen182

    Joined:
    Aug 21, 2012
    Posts:
    97
    The green ray is being drawn from the follow.position in the direction represented by targetPosition, which I don't think is what you want. If you want to draw a ray from the follow.position in the direction the targetPosition is from the follow position then you would do:

    Code (csharp):
    1. Debug.DrawRay(follow.position, targetPosition - follow.position, Color.green);
    I'm not certain how you're calculating the targetPosition, it looks like you might be getting some weird results due to precedence. But I'm not sure what exactly you're trying to accomplish so it could be right. Whatever the case, the code above will draw a ray in the direction of the targetPosition from the follow.position. If it's still off, check your math on targetPosition.