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 cariaga · Feb 24, 2014 at 09:56 PM · rotationangledegree

check if front left, front right, back right, back left, Angle of two transforms?

how do you check if enemy is on back left of the character something like that i really want to know how its done something like if(withn 0 and 90)//front right if(within 90 and 180)//back right if(within 180 and 270)//back left if(within 270 and 360)//front left

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

2 Replies

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

Answer by robertbu · Feb 24, 2014 at 10:17 PM

You can transform your enemy's position into local coordinates. Things with a negative 'z' will be behind the pivot position. Things with negative 'x' will be on the left. Here is a bit of code to demonstrate. It assumes that when the rotation is (0,0,0), the front of your object is facing positive 'z'. Play with in a new scene to start. Attach it to an object, create a target object in the scene, then drag and drop the target object on the 'target' variable in the script. While running, move the target object around the scene.

 #pragma strict
 
 var target : Transform;
 
 function Update() {
     var output : String;
     var pos = transform.TransformPoint(target.position);
     
     if (pos.z < 0) 
         output = "Back ";
     else 
         output = "Front ";
         
     if (pos.x < 0)
         output += "Left";
     else 
         output += "Right";
         
     Debug.Log(output);
 }
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 cariaga · Feb 24, 2014 at 11:34 PM 0
Share

here was my version BUT yours works better i'm sure :) thank you!

 public Transform p1; // the magnetic pole
     public Transform p2;
     
  
 /*void Update () {
         /*float angle  = $$anonymous$$athf.Atan2(p2.position.y-p1.position.y, p2.position.x-p1.position.x)*180 / $$anonymous$$athf.PI;//left and right
         var relativePoint = p1.InverseTransformPoint(p2.position);//back and front
         
         
         
         //print(angle+" "+relativePoint);
     
 
         
         if(angle<0){
             print ("Object is to the Front");
         }
         if(angle>0){
             print ("Object is to the back");
         }
            if (relativePoint.x < 0.0){
                print ("Object is to the left");
         }
             if (relativePoint.x > 0.0){
             print ("Object is to the right");
         }*/
         
  
 //}
avatar image
0

Answer by rcober · Aug 09, 2014 at 04:41 PM

For future reference clarity , as you already stated , you should use InverseTransformPoint to transform to local space:

 Vector3 local_pos = transform.InverseTransformPoint(camera.transform.position);
 if (local_pos.z < 0) 
   Debug.Log ("Back ");
 else 
   Debug.Log ("Front ");
 
 if (local_pos.x < 0)
   Debug.Log ("Left ");
 else 
   Debug.Log ("Right ");
 
 
 
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

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 to use the lerp function with rotation? (C#) 1 Answer

local rotation per axis between frames? 1 Answer

How do I add force to an object based on my Camera's cureent Y rotation (C#) 2 Answers

rotation x changes all angles? 3 Answers

Make object only rotate in 90 degree increments 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