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
1
Question by darkcookie · Jan 04, 2014 at 09:21 AM · 2drotationfollowlookattarget

Rotate Object to face player 2d

I'm making a 2D platformer , but i have a problem making the enemy face the player. I was able to make the main character face left or right by rotating its plane mesh 180 so its in reverse depending on what key is pressed a,or d.....I want the toaster/ enemy to face the player if the its within a certain distance. In other word rotate the toaster to (90,0,0) on the xyz axis if the character is on the Right side of the toaster , and (90,180,0) if its on the left of the toaster. .... alt text alt text

I made this script to check the distance between the Enemy and player and depending on the y value of the rotation it will flip to 180 or 0 on the y ,

the thing is that it doesnt work properly what am I doing wrong ? How do i make the Toaster face the player?

 var NoticeDistance = 10;
 var Player: Transform;
 var RotatedToRight= false;
 var RotatedToLeft= true;
 
  
 function LateUpdate(){
 
 var relativePoint = transform.InverseTransformPoint(Player.position);
 
 if(Vector3.Distance(transform.position,Player.position) <= NoticeDistance){
        
          if (relativePoint.x < 0.0 && Input.GetKeyUp("d") ){
         print ("Object is to the Right");
         transform.eulerAngles = Vector3(90,0,0);
        /*   if(transform.rotation.eulerAngles.y >= 160 ){
             print("Y rotation is about 180");
             RotateRight();
           }*/
        
         }
         
     else if (relativePoint.x > 0.0 && Input.GetKeyUp("a") ){
         print ("Object is to the Left");
         transform.eulerAngles = Vector3(90,180,0);
       /*  if(transform.rotation.eulerAngles.y <= 5 ){
           print("Y rotation is about 0");
           RotateLeft();
           } */
         }
         
         
     else{
         print ("Object is directly ahead");
         } 
     }
          
 }
 function RotateLeft(){
       //     yield WaitForSeconds (3); //Debug yeild
            transform.eulerAngles = Vector3(90,180,0);
            RotatedToLeft = true;
      //      yield WaitForSeconds (5);//Debug yeild
            RotatedToRight = false;
 }
 function RotateRight(){
      //      yield WaitForSeconds (3);//Debug yeild
            transform.eulerAngles = Vector3(90,0,0);
            RotatedToRight = true;
      //      yield WaitForSeconds(5);// Debug yeild
            RotatedToLeft = false;          
 }


screenshot (16).png (172.2 kB)
screenshot (17).png (187.1 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
1
Best Answer

Answer by BigRoy · Jan 04, 2014 at 10:56 AM

I think this is where you're misunderstanding what it's doing:

 var relativePoint = transform.InverseTransformPoint(Player.position);

The **InverseTransformPoint** actually turns the point into local space of the object. Therefore if the object is rotated then the point will be converted as well. When you check the point with this code make sure to check whether the point is 'in front or behind' instead of 'to the left or the right'. Therefore using this technique you're basically SWITCHING sides when it's behind instead of always turning left if x < 0. Because the relative point is either in front or behind.

Also if it's scaled in the X-axes the point will be much closer in local space. (Literally stretch and squash of the relative point.)

To be consistent you'll probably be better of to do:

 var direction : Vector3 = Player.position - transform.position.

You then get a vector pointing from the transform to the Player. Which should be exactly what you need as your code that checks the position checks for this difference already correctly (I just quickly looked at the code)

Another sidenote

You're using the transform many times. Because it isn't cached or stored it looks for the transform component each time. Have a look at this: Are .gameObject and .transform both using GetComponent() in the background? There's a speed difference, which might be neglible in this case but can become obvious if you're going to expand this code a lot.

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 darkcookie · Jan 04, 2014 at 11:27 AM 0
Share

Wow thank you it worked perfectly but how can i get a smooth transition between left and right because if i stand on top of the enemy ,It flip flops really fast and trows me off down the z axis, Also, thank you for advising me in the correct path (learning to code) ill look into that transform problem.

avatar image darkcookie · Jan 04, 2014 at 11:49 AM 1
Share

O$$anonymous$$ i made it smoother by creating a gap between front and back so the point can be in that gap and the character will not flip .. thanks !!

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

[Help] Object flips around when using transform.Rotate 3 Answers

Lerp toward object while keeping a constant speed 3 Answers

camera follow problem-unity 4.3 2d 1 Answer

2D sprite rotation with velocity and interpolation 1 Answer

Camera script 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