Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jorg van immerseel · May 25, 2014 at 02:05 PM · c#raycast

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); 
         }
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jorg van immerseel · May 25, 2014 at 02:02 PM 0
Share

srry will do

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jorg van immerseel · May 25, 2014 at 02:54 PM 0
Share

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?

avatar image Fornoreason1000 · May 26, 2014 at 05:16 AM 0
Share

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);
avatar image jorg van immerseel · May 26, 2014 at 04:57 PM 0
Share

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

avatar image Fornoreason1000 · May 27, 2014 at 08:50 AM 0
Share

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;
avatar image jorg van immerseel · May 27, 2014 at 04:41 PM 0
Share

ok thanks i will test if that works :) and let you know how it turns out :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I turn this into a Lerp function 1 Answer

Is there any reason why I can't use 2 c# scripts on my Player? 3 Answers

Controlling when a script is enabled with another Script 1 Answer

Multiple Cars not working 1 Answer

Can't find fix for buggy camera 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges