- Home /
Raycast line in c#
I know there are thousands of raycast answers but i cant' find my solution in c# in all of those answers. I want to let the line that is drawn in the last line of the code appear in my gamewindow en not only in the scene wiondow. but i don't know how to dow that. can someone help me to reshape my code?
thanks jorg
using UnityEngine;
using System.Collections;
public class RaycastingScript : MonoBehaviour {
Ray ray;
RaycastHit hit;
public int number;
void Start () {
// Initialise ray
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Print out the current number value to the console window
Debug.Log("Target is currently: " + number);
}
void Update () {
Selection();
}
void Selection() {
// Use Input.GetKeyDown() for single clicks
if(Input.GetKeyDown(KeyCode.Mouse0))
{
// Reset ray with new mouse position
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit)) {
if(hit.transform.tag == "Yellow") {
number = 5;
}
if(hit.transform.tag == "Red") {
number = 4;
}
if(hit.transform.tag == "Blue") {
number = 3;
}
if(hit.transform.tag == "Black") {
number = 2;
}
if(hit.transform.tag == "White") {
number = 1;
}
// Reset number to zero if no object selected
if (hit.transform.tag == "Untagged") {
number = 0;
}
Debug.Log("target is currently: " + number);
}
}
// Draw a red line from camera to selected object in Scene window
Debug.DrawLine(ray.origin, hit.point, Color.green);
}
}
Answer by Fornoreason1000 · May 25, 2014 at 02:10 PM
Instead of using Debug.drawline, use the line Renderers class... They work pretty much the same except line renders use array of positions instead of two points and you can change the material.
Simply instantiate a new line Renderer , assign the two points to its anchors and you are basically done.
Let me know if you need extra info . Good luck.
can you maybe wright the code or something because i don't realy know how to start with that.
and does that line that i create also change position like the debug.drawline?
Ok First you need to create a line render and make reference to it in your script. then you set the renders points to your Ray's origin and your hit point
Here's a way to create a Linerenderer, because LineRenderer is a component it will need a GameObject as well.
//Create a new Line render and sets up the first two points
public LineRenderer CreatewNewLine(Vector3[] points) {
//Create the GameOBject and name it
GameObject go = new GameObject();
go.name = "$$anonymous$$yLineRenderer";
//Add a lien renderer to the gameObject and store its isntance as "line"
var line = go.AddComponent("LineRenderer") as LineRenderer;
//Set the number of points
line.SetVertexCount(2);
//Use world space
line.useWorldSpace = true;
//Sets the positions of the first two points
line.SetPosition(0,points[0]);
line.SetPosition(1,points[1]);
return line;
}
now this function should be called in start to initialize it public void Start() {
//Create our line, we set its points to zero because we are only initializing it.
myLine = CreateNewLine(new Vector3[] {Vecotr3.zero, Vector3.zero})
}
Now that you have done that you can replace Debug.DrawLine()
with
//Draw a red line from camera to selected object in SceneWindow
myLine.SetPosition(0,ray.origin);
myLine.SetPosition(1,hit.point);
my.Line.SetColors(Color.red, Color.green);
thanks man thats just amazing :)
i have done it like this but i still get some things in red. did i do something wrong?
http://prntscr.com/3msn5v code part 1 printscreen http://prntscr.com/3msnfa code part 2 printscreen
okay you need a memeberwise variable to assign your line to... otherwise you the compiler has no idea what you are talking about when you reference "myLine" :)
Ray ray;
RaycastHit hit;
public int name;
LineRenderer myLine;
ok thanks i will test if that works :) and let you know how it turns out :)