Skip to main content
Added an update
Source Link
Milan
  • 113
  • 4

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)

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)

added 1 character in body
Source Link
Milan
  • 113
  • 4

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 iI 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. 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?

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?

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?

Source Link
Milan
  • 113
  • 4

Why does the Debug.DrawRay return these weird lines

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