Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Raymund2016 · Sep 28, 2016 at 03:22 PM · c#raycastobjectsdistance check

How to get distance between two object by using a pinpoint? Or Tag?

I was able to get distance of two objects using raycast but I need to get distance using the raycast to pin point one object then to another game object and get the distance between those two.

Here is a screenshot:

alt text

Here is my code so far.

using UnityEngine; using System.Collections;

public class RaycastDistance : MonoBehaviour {

 public GUIText distanceText;//This will hold the value of distance from Point A
 public GUIText distanceBText;//This will hold the value of the distance from Point B
 public Transform other;

 private float distance;

 void Start ()
 {
     distance = 0;//Distance initial value set to 0
     SetDistanceText ();//Use to call the function SetDistanceText
     distanceBText.text = "";

 }

 // Update is called once per frame
 void Update ()
 {

     RaycastHit hit;
     Vector3 up = transform.TransformDirection (Vector3.forward);//This where the raycast should point either forward,up,down,etc.0
     Debug.DrawRay (transform.position, up * 100, Color.green);//Raycast draws line in green

     if (Physics.Raycast (transform.position, up, out hit)) 
     {
         distance = hit.distance;
         distanceText.text = "Distance: " + distance.ToString();
         distanceBText.text = "YOU GOT THE DISTANCE MEASUREMENT!!";
         Debug.Log("This distance is " + hit.distance);
     }
 
 }

 void SetDistanceText()
 {
     distanceText.text = "Distance: " + distance.ToString();
 }

}

Please really need help. Thanks.

screen-shot-2016-09-28-at-120946-pm.png (132.5 kB)
screen-shot-2016-09-28-at-121003-pm.png (133.8 kB)
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
0

Answer by Dave29483 · Sep 29, 2016 at 07:56 AM

It may be better to have the raycast simply return and store the transform of the last two objects selected. Then you can do the following once both transforms are assigned;

distance = (target.position - source.position).magnitude;

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Select an Enemy C# 1 Answer

Distribute terrain in zones 3 Answers

Click On/Off Manager - C# 2 Answers

Reverse object position order 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