1
\$\begingroup\$

I am working on a small feature of a 2D mobile game where a indicator points towards a object in 2D space. I am trying to make the indicator turn towards the object, however the vector I am trying to construct that points towards the object is off a bit.

When I decided to draw a debug ray from the indicator position to the objects position it was also off a bit. I then decided to draw the debug ray the other way around which gave me a different line. If my understanding is correct, if you switch the starting point and the end point of a ray the line should stay exactly the same right?

using UnityEngine;
using System.Collections.Generic;

public class Tracker : MonoBehaviour {

public GameObject goToTrack;
private List<Renderer> renderers = new List<Renderer>();

void Start(){
    foreach (Transform child in transform) {
        renderers.Add (child.gameObject.GetComponent<Renderer>());
    }
}

void Update () {

}

void OnGUI(){
    Vector3 v3Screen = Camera.main.WorldToViewportPoint(goToTrack.transform.position);

    // if the target is within the viewable screen the indicator shouldn't be rendered)
    if (v3Screen.x > -0.01f && v3Screen.x < 1.01f && v3Screen.y > -0.01f && v3Screen.y < 1.01f) {
        foreach (Renderer renderableObject in renderers) {
            //renderableObject.enabled = false;
        }
    }
    else
    {
        foreach (Renderer renderableObject in renderers) {
            //renderableObject.enabled = false;
        }

        v3Screen.x = Mathf.Clamp (v3Screen.x, 0.01f, 0.99f) -0.04f;
        v3Screen.y = Mathf.Clamp (v3Screen.y, 0.01f, 0.99f) -0.04f;
        transform.position = Camera.main.ViewportToWorldPoint (v3Screen);

        Debug.Log ("Target planet position: " + goToTrack.transform.position.ToString());
        Debug.DrawRay(Camera.main.ViewportToWorldPoint (v3Screen), goToTrack.transform.position, Color.red, 5.0f);
        Debug.DrawRay(goToTrack.transform.position, Camera.main.ViewportToWorldPoint (v3Screen), Color.green, 5.0f);

        //Debug.Log ("Tracker angle: " + angle);
        //transform.Rotate(0,0, angle, Space.Self);
    }

}
}

Output: Screenshot of the rays

UPDATE: I have noticed that when i substract the second vector with the first vector of the Debug.DrawRay it draws the ray like i want it to.

From this I am taking that the Debug.DrawRay function draws the second vector on top of the first vector. (kind of like the the first vector is Vector(0,0,0) and the second vector is the actual ray you want to draw)

\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

You're drawing Rays, not Lines. This means that the second parameter is a direction not a point in space.

https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html

Draws a line from start to start + dir in world coordinates.

That's why your two debug lines aren't the same. If you want to draw lines, use Debug.DrawLine() instead.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .