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 kingkonglee · May 22, 2014 at 08:24 AM · 2drotationgameobjectgameenemy

Enemy Rotation Problem

Hey guys, I have a problem here. I have been trying to fix it but i just couldn't get it. I am still new in unity, started using unity about 7 weeks already. Okay, so i have this script of my enemy following my main character, and what i want it to do is to rotate as well, but it seems like the enemy is rotating but in a weird way. here is the code Thank you so much for helping.

 var Player : Transform;
 var MoveSpeed = 4;
 var MaxDist = 100;
 var MinDist = 50;
 private var tr : int = 180;
 private var moveDirection = Vector3.zero;
  
  
  
 function Start () 
 {
 
 }
  
 function Update () 
 {
     //transform.LookAt(Player);
      var dist: float = Vector3.Distance(transform.position,Player.position);
      var dir: Vector3 = Player.position - transform.position;
      //Debug.Log(dir);
      dir.Normalize();
      
       if(dist > MinDist)
     {
  
          transform.position += dir * MoveSpeed * Time.deltaTime;
  
          if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
          {
                transform.eulerAngles.y += (transform.eulerAngles.y - tr) / 2;
          } 
 
     if(dir.x>0){
         //looking to the right
         tr = 180;
         
     } else {
         tr = -180;
         
     }
     
     // if(dir.x<0){
         //looking to the left
         //tr = 0;
         //transform.eulerAngles.y += (transform.eulerAngles.y + tr) / 2;
         
     }
     
     
 }
         
    
  
 
    
   
 
Comment
Add comment · Show 4
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 kingkonglee · May 22, 2014 at 11:53 AM 0
Share

I want to rotate the enemy to the player, which i have already tag the enemy to the player. It rotates weird is like, when the player is nearby the enemy and just keep pressing left and right the enemy will turn but it disappeared. @supernat

avatar image robertbu · May 22, 2014 at 03:33 PM 0
Share

I see you commented out a 'transform.LookAt(Player);' Why did that not work for you. Is it because 1) it did not look at the player, or 2) it was an immediate rotation and you wanted to rotate over time, or 3) it rotated on more than one axis and you only wanted to rotate on the 'y' axis? The fix will depend on the reason.

avatar image kingkonglee · May 23, 2014 at 04:40 AM 0
Share

@robertbu It did look at the player when I put the 'transform.LookAt(Player);' just that it rotate everywhere. I only want it to rotate on X axis. Like when the player turn right it will snap on right as well

avatar image kingkonglee · May 24, 2014 at 11:59 AM 0
Share

@robertbu Sorry I didn't mention it was 2D now it doesn't do the weird turning, but it still doesnt look at where the player is looking at :(

http://s2.postimg.org/7wfcnusd5/Untitled.jpg

@Pyrian Do i need to rotate my whole scene?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · May 23, 2014 at 05:55 AM

You say, "I only want it to rotate on the X axis," but your existing code is rotating the object on the y axis. So I'm going to assume you really want a 'y' axis rotation. If so, this is likely what you are looking for:

 function Update ()  {
     var lookPos = Player.position;
     lookPos.y = transform.position.y;
     transform.LookAt(lookPos);
     
     var dist: float = Vector3.Distance(transform.position,Player.position);
     var dir: Vector3 = Player.position - transform.position;
     
     dir.Normalize();
  
      if(dist > MinDist) {
          transform.position += dir * MoveSpeed * Time.deltaTime;
  
          if(Vector3.Distance(transform.position,Player.position) <= MaxDist) {
                transform.eulerAngles.y += (transform.eulerAngles.y - tr) / 2;
          } 
     }
 }

It works by putting the Players position on the same 'y' as this object, resulting in a rotation only on the y axis.

Comment
Add comment · Show 8 · 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 kingkonglee · May 24, 2014 at 01:13 AM 0
Share

@robertbu the enemy become like this and it doesn't rotate to where my player is facing

http://s17.postimg.org/pmzjqlqtr/game.jpg

avatar image Pyrian · May 24, 2014 at 02:58 AM 0
Share

In 2D, we usually only rotate on the Z-axis.

avatar image robertbu · May 24, 2014 at 06:44 AM 0
Share

I did not catch it was 2D. I'm not sure of all of your expected behavior. Here is a rewrite for 2D:

 #pragma strict
 
 var Player : Transform;
 var $$anonymous$$inDist : float;
 var $$anonymous$$oveSpeed : float;
 var $$anonymous$$axDist : float;
 
 function Update ()  {
     var dist: float = Vector3.Distance(transform.position,Player.position);
     var dir: Vector3 = Player.position - transform.position;
     
     dir.Normalize();
  
      if(dist > $$anonymous$$inDist) {
          transform.position += dir * $$anonymous$$oveSpeed * Time.deltaTime;
  
          if(Vector3.Distance(transform.position,Player.position) <= $$anonymous$$axDist) {
              var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
                 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);    
          } 
     }
 }

Note the rotation is done by these two lines:

 var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);    
avatar image kingkonglee · May 25, 2014 at 07:50 AM 0
Share

@robertbu Sorry i didnt mention it was 2D in the first place, ermm it doesn't turn weird anymore, but now the enemy is not looking where the player is looking

avatar image robertbu · May 25, 2014 at 03:58 PM 0
Share

The above code assumes that the side that is considered the front side is on the right. If the side is up, you need to add 90 to 'angle' before calling AngleAxis().

Show more comments

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

23 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

Related Questions

how to tell a game object to move between 2 points(A,B) along x axis , right to left, and then move left to right back to its first position ??? 3 Answers

2D enemy AI 1 Answer

How to cover buttons with game objects? 1 Answer

Reverse object position order 1 Answer

can't detect what player collides with, tried everything and all other posts... 2 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