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 /
  • Help Room /
avatar image
0
Question by Xeong-Hu · Sep 20, 2015 at 09:54 AM · collisiondistancedegrees

How to determine angle degrees distance of one object from another?

Alright here's what I mean. Say I have this flashlight right? And I have it Flashing light in a direction where I want to see. The Flashing light is the head point. Like the front.

So then I also have this object directly behind me.

So because the object is directly behind that would mean it's 360 degrees angle away from my Flashing Light the head point.

But as I rotate around, the degrees distance from my head point to the object becomes narrower and lesser. Like 270,180,90,45,0 simple like that.

Does anyone know how I would go about this?

I made a box collider to have it be treated as my Flashing Lights Head Point but I don't know to measure the distance in degrees and so on.

Any help would be good.

: )

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
Best Answer

Answer by burtonposey · Sep 20, 2015 at 10:23 AM

I am pretty sure that directly behind you would be 180 degrees.

You want to use Vector3.Angle . It takes in a from Vector3 and a to Vector3 and returns the angle between the two. If I understand you correctly, you'll have your head angle which is, at the start, this flashing light reference point. If you're staring at that, it's effectively your Camera's transform.forward. This is your from. Your to is your target's position - the camera's transform.position.

So if this script were to be attached to your camera it would look like this:

 using UnityEngine;
 using System.Collections;
 
 public class AngleTest : MonoBehaviour {
 
     public Transform targetTransform;
 
     // Update is called once per frame
     void Update ()
     {
         if (targetTransform != null)
         {
             float angleToTarget = Vector3.Angle(transform.forward, targetTransform.position - transform.position);
             Debug.Log("Angle to target is: " + angleToTarget);
         }
     }
 }

Comment
Add comment · Show 2 · 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 Xeong-Hu · Sep 20, 2015 at 10:48 AM 0
Share

Aww Yeah lol. 180.

Thanks though dude.

I tried this before but I didn't think I needed the transform.forward as well.

Well this does work, in some.. way.

For some reason when I distant my object farther and closer the numbers still range up and down. Like it's good. but as I get dead closer while in front of it, it ranges to about 90 until it totally reaches the other side. and as I get farther while in front, it ranges around 0. but when i'm directly in front of it at a good distance, it becomes around 6 - 8.

It's weird. I tried Localposition but that seem to only decrease the number by 2 points.

Hmm.. Any idea why it's doing this? $$anonymous$$ind of annoying.

avatar image Xeong-Hu · Sep 20, 2015 at 01:42 PM 0
Share

Haha Nvm dude, this did it. I did a standard Cube Test and I figured out why it did that.

It was because both objects weren't lined up in the same Z Axis (Up & Down Axis)

Thanks $$anonymous$$an!

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object stop when the distance between it and another object reaches 0 ? 1 Answer

Measure distance between a fixed object and a moving prefab? 0 Answers

Colliding with other objects. How to stop it? 1 Answer

How to make wall go up, when you collect cube? 0 Answers

Adding points to Collider as element of a list. 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