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 AR_Rizvi · Jan 30, 2014 at 12:01 PM · raycasthitpoints

Draw Line In GameView Point to point

I want to draw a line in Game View On Each Collision Point on a mouse click Like Every Where i click it draws a line from prev point to currnt point Measuring the distance btween these point IS it possible? if yes then help me get started i m experimenting with raycast rightnw Any help wud be Greatly Appriciated `

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by clunk47 · Jan 30, 2014 at 10:13 PM

For this example, I'm using a Ray, RaycastHit, and Vector3.Distance. I'm simply creating a new LineRenderer object each time you click on 2 different colliders. This measures the distance between the hit point of each transform, not just the transforms themselves. If you just want the transforms' positions to be measured, remove h0 and h1 and just use p0.position and p1.position as the Vector3.Distance parameters. I just used a scene w/ some cubes for testing.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Example : MonoBehaviour 
 {
     Ray ray;
     RaycastHit hit;
     Transform p0 = null;
     Transform p1 = null;
     Vector3 h0;
     Vector3 h1;
     int i = 0;
     Transform container;
     float distance;
     string label = "";
     
     void Start()
     {
         container = new GameObject("Lines").transform;
     }
     
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(0))
         {
             if(!p0 && !p1)
             {
                 p0 = hit.transform;
                 h0 = hit.point;
                 return;
             }
             
             if(p0 && !p1)
             {
                 p1 = hit.transform;
                 h1 = hit.point;
             }
                 
             if(p0 && p1)
             {
                 LineRenderer line = new GameObject("Line " + i.ToString()).AddComponent<LineRenderer>();
                 line.transform.parent = container;
                 line.SetWidth(0.025F, 0.025F);
                 line.SetColors(Color.red, Color.green);
                 line.SetVertexCount(2);
                 line.SetPosition(0, h0);
                 line.SetPosition (1, h1);
                 distance = Vector3.Distance(h0, h1);
                 label = "Distance between hit point 0 of " + p0.name + " and hit point 1 of " + p1.name + " = " + distance.ToString ();
                 p1 = null;
                 p0 = null;
             }
         }
     }
     
     void OnGUI()
     {
         if(GUILayout.Button ("Reset"))
         {
             foreach(Transform lineObject in container)
             {
                 Destroy(lineObject.gameObject);
             }
         }
         
         GUILayout.Label(label);
     }
 }
 


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 AR_Rizvi · Jan 31, 2014 at 06:17 AM 1
Share

Thankyou So $$anonymous$$uch for your input It is soo help full but a littel problem is that i want the distance of two hit points on a same mesh sorry for nt mentioning it in my question can you give any help on this Otherwise you script is working marvlously Thanks for support

avatar image clunk47 · Jan 31, 2014 at 06:30 AM 0
Share

Edited answer.

avatar image AR_Rizvi · Jan 31, 2014 at 07:06 AM 1
Share

Thankyou so much man It is working awsmly :) You Rockx

avatar image clunk47 · Jan 31, 2014 at 07:16 AM 0
Share

You're most welcome :D

avatar image clunk47 · Jan 31, 2014 at 07:17 AM 2
Share

Note you can remove the SetColors() instruction, since I didn't add a material to the LineRenderer in this code. I'll edit here in a bit to take out anything unnecessary.

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

racing game raycast problem 2 Answers

How do I change the value of a tag with raycasting? 0 Answers

C# Check Physics.Raycast Once 0 Answers

Calling function in a script based on raycast hit 1 Answer


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