Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Razputin · Apr 03, 2016 at 03:59 AM · transformvector3angle360-degrees

Finding the Angle Between Two Objects in 360 Degrees.

Trying to return values based on angle of object relevant to player but I can't get it to do 360 degrees it only does 180. I searched for how to make it 360 degrees online but still haven't got it working. I know I need to differentiate between the left side and the right side which is why I added a right bool and the line if (Vector3.Angle(target.right, target.position - target.position) > 90f) right = false; else right = true; but it always returns right as true, any help would be appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAngle : MonoBehaviour {
 
     public Transform target;
     public GameObject player;
     public bool right;
 
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         target = player.transform;
     }
     void Update()
     {
         Vector3 targetDir = target.position - transform.position;
         Vector3 forward = transform.forward;
         float angle = Vector3.Angle(targetDir, forward);
         if (Vector3.Angle(target.right, target.position - target.position) > 90f) right = false; else right = true;
         if (angle < 45.0f && angle > 0)
         {
             Debug.Log("45");
         }
         if (angle < 90.0f && angle > 45.0f)
         {
             Debug.Log("90");
         }
         if (angle < 135.0f && angle > 90.0f)
         {
             Debug.Log("135");
         }
         if (angle < 180.0f && angle > 135.0f)
         {
             Debug.Log("180");
         }
         if (angle < 45.0f && angle > 0 && right == true)
         {
             Debug.Log("225");
         }
         if (angle < 90.0f && angle > 45.0f && right == true)
         {
             Debug.Log("270");
         }
         if (angle < 135.0f && angle > 90.0f && right == true)
         {
             Debug.Log("315");
         }
         if (angle < 180.0f && angle > 135.0f && right == true)
         {
             Debug.Log("360");
         }
     }
 }
 


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 Razputin · Apr 03, 2016 at 03:12 PM 0
Share

Bumpingggg

3 Replies

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

Answer by Eno-Khaon · Apr 03, 2016 at 04:55 PM

There's a much simpler way to determine whether the angle is to right or to the left when your axes are clearly defined.

When you calculate a Dot Product, two vectors facing within the same 180 degrees of each other result in a positive value, two vectors facing opposite directions are negative and perpendicular vectors results in zero.

How is this relevant? Well, If the dot product of transform.right and targetDir vectors is greater than zero, it's to the right. If it's less than zero, it's to the left. Finally, if it is zero, it's neither left nor right, but is either exactly in front of or exactly behind instead.

Now, this has nothing to do with why your current setup isn't working, regardless. Why isn't yours working at the moment?

Well, it's due to a simple error:

 if (Vector3.Angle(target.right, target.position - target.position) > 90f)

target.position - target.position == Vector3.zero; You've been testing the angle between the target's right and Vector3.zero. You likely should have been comparing between transform.right and targetDir instead.

Comment
Add comment · Show 1 · 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 Razputin · Apr 03, 2016 at 05:02 PM 0
Share

Omg thank you I didn't know about dot product it's SOOOOO much easier.

avatar image
1

Answer by Razputin · Jun 21, 2021 at 01:33 AM

I'm coming back to this years later because it's one of the top google answers. Here's an easy solution.

     float angle = Vector3.SignedAngle(vectorOne, vectorTwo, Vector3.up); //Returns the angle between -180 and 180.

     if(angle < 0)
     {
         angle = 360 - angle * -1;
     }

There you go. Angle will now be from 0-360. No need to over complicate things.

Comment
Add comment · Show 1 · 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 Lucas · Jul 03, 2021 at 04:14 PM 1
Share

Best solution ever, thank you. I used it to deter$$anonymous$$e if my player is walking left/right while looking forward for my Animator. Here's the code:

 Vector3 vec1 = new Vector3(characterController.velocity.x, 0, characterController.velocity.z);
 
 Vector3 vec2 = new Vector3(FPSCamera.transform.forward.x, 0, FPSCamera.transform.forward.z);
 
 float animatorDirection = Vector3.SignedAngle(vec1, vec2, transform.up);
avatar image
-1

Answer by MrBalin · Feb 04, 2020 at 12:21 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FindDirections : MonoBehaviour
 {
     [SerializeField] GameObject Target;
 
     [SerializeField] Vector2 Fwd_MinMax = new Vector2(50f, 140f);
     [SerializeField] Vector2 Side_MinMax = new Vector2(50f, 140f);
 
     enum MyEnum
     {
         Bow,
         Left,
         Right,
         Stern,
         UnAssigned
     }
     MyEnum targetDirection = MyEnum.UnAssigned;
 
     private void Update()
     {
         WhereIsTarget();
     }
 
     void WhereIsTarget()
     {
         Collider myCollider = transform.GetComponent<Collider>();
         Vector2 myColliderCenter = new Vector2(myCollider.bounds.center.x, myCollider.bounds.center.z);
 
         Vector2 target_asV2 = new Vector2(Target.transform.position.x, Target.transform.position.z);
         float angle = Vector2.Angle(Vector2.up, target_asV2 - myColliderCenter);
 
         if(angle <= Fwd_MinMax.x)
         {
             Debug.Log("Target in front");
             targetDirection = MyEnum.Bow;
         }
 
         if(angle > Fwd_MinMax.x && angle < Fwd_MinMax.y)
         {
             float angle_XAxis = Vector2.Angle(Vector2.right, target_asV2 - myColliderCenter);
 
             if(angle_XAxis <= Side_MinMax.x)
             {
                 Debug.Log("Target right");
                 targetDirection = MyEnum.Right;
             }
             if(angle_XAxis >= Side_MinMax.y)
             {
                 Debug.Log("Target left");
                 targetDirection = MyEnum.Left;
             }
         }
 
         if(angle >= Fwd_MinMax.y)
         {
             Debug.Log("Target behind");
             targetDirection = MyEnum.Stern;
         }
     }
 
     private void OnDrawGizmos()
     {
         if(Target != null)
         {
             Vector2 tempColliderCenter = new Vector2(transform.GetComponent<Collider>().bounds.center.x, transform.GetComponent<Collider>().bounds.center.z);
             Vector3 tempPos = new Vector3(tempColliderCenter.x, 0, tempColliderCenter.y);
 
             Gizmos.DrawSphere(tempPos, .3f);
             Gizmos.DrawLine(tempPos, Target.transform.position);
         }
     }
 }
 
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

49 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 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

Make a side of an object LookAT another object 1 Answer

Problems with Vector3.angle and angle to target 1 Answer

Rotate an Object based on shorter angle 1 Answer

Rotate floor plane in-game via C# script 1 Answer

Animating in Unity Rotation snaps to last rotation key 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