- Home /
Debug.drawline?
Hello!
I am making a 2D top down shooter. Right now i am working on the shooting mechanism and decided to go with raycasting. What i want to do is first check so that my ray is going the way i want using Debug.Drawline. I have been trying to figure out how to make a straight line from the fire point to the mouse position for a few hours but i cant figure it out really...
Here is what i have so far:
using UnityEngine;
using System.Collections;
public class Winchester : MonoBehaviour {
private Vector3 firePointPosition;
public Vector3 Mouseposition;
private GameObject firePointObj;
private Transform firePoint;
private float mouseX;
private float mouseY;
void Start ()
{
firePointObj = GameObject.FindGameObjectWithTag ("FirePoint");
firePoint = firePointObj.transform;
}
void Update()
{
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
Mouseposition = Camera.main.WorldToScreenPoint (new Vector3(mouseX, mouseY, -10));
firePointPosition = new Vector3 (firePoint.position.x, firePoint.position.y,0);
Debug.DrawLine(firePointPosition,firePointPosition-Mouseposition);
}
}
i now get an infinite line (which is fine) but it follow the mouse movement. Any tips?
Thank you!
/Taegos
And dont start $$anonymous$$ouseposition with a capital letter. All variable names should start with a lower letter.
Thank you! This made it work only when my player is standing still, very wierd... Its a step on the way though :)
Sorry I thought Debug.DrawLine was a Ray. Change
Debug.DrawLine(firePointPosition,firePointPosition-$$anonymous$$ouseposition);
to
Debug.DrawLine(firePointPosition,$$anonymous$$ouseposition);
Answer by Positive7 · Aug 01, 2015 at 11:34 PM
using UnityEngine;
using System.Collections;
public class Winchester : MonoBehaviour {
public Vector3 mouseposition;
void Update()
{
if (Input.GetMouseButton(0)) {
mouseposition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
mouseposition.z = 10;
Debug.DrawLine(transform.position, mouseposition, Color.red, 10f, true);
}
}
}
What if i wanted the line to have a set pixels of length or infinite length? So that the line follows the mouse direction but continues at a desired length? Is that possible do you think?
change mouseposition.z = 10; to 100 or whatever length you want. Infinite won't work. For that you will have to use Raycast or maybe Line.Renderer should work too.
using UnityEngine;
using System.Collections;
public class Winchester : $$anonymous$$onoBehaviour {
public Vector3 mouseposition;
public float visibilityTime = 10f;
void Update()
{
if (Input.Get$$anonymous$$ouseButton(0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit,$$anonymous$$athf.Infinity)){
mouseposition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
mouseposition.z = hit.collider.transform.position.z;
Debug.DrawLine(transform.position, mouseposition, Color.red, visibilityTime, true);
}
}
}
}