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 Reikari · May 09, 2013 at 04:40 AM · rotatelookatwrong

Enemy lookAt Player

Hey Guys! I have a problem.

i animated a character, and made a simple AI Script, that the enemy first stares at me, and than (when i am near him) he runs at me. The Problem is: he walks sideways. I looked for answers from others who have the same Problem as me, but it didn't helped. I tried to rotate the character in Cinema4D to look in front of the camera, but this doesn't seem to affect the object. Transforming the object in the Inspector doesn't help too. He always runs and looks at the wrong direction. Here's the code i used:

 var speed = 5.0f;
     var focusDist : float = 10.0f;
      
     var player : Transform;
      
     private var enemyToPlayer : Vector3;
      
     function Update()
     {
     zombiToPlayer = player.transform.position - transform.position;
     var dist = zombiToPlayer.magnitude;
      
     if( dist <= focusDist )
     RunTowardPlayer();
     else
     Idle();
     }
      
     function RunTowardPlayer()
     {
     transform.rotation = Quaternion.LookRotation(enemyToPlayer);
      
     gameObject.GetComponent( CharacterController ).Move( enemyToPlayer.normalized * speed * Time.deltaTime );
     animation.CrossFade( "WFront", 0.3 );
     }
      
     function Idle()
     {
     animation.CrossFade( "Idle", 0.3 );
     }

Please help me!

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

Answer by TonyLi · May 09, 2013 at 02:55 PM

In Update(), add

 transform.LookAt(player);

Note that the transform property isn't a variable. It actually calls GetComponent() every time you reference it. If you need to optimize Update(), consider storing the value of transform in a variable once in Start() and using that variable instead.

As you enhance this, you'll probably want to use Mathf.SmoothDamp() to gradually rotate toward the player instead of Look(), which changes direction immediately. But LookAt() will get you started.

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

14 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

Related Questions

Autorotation to tween gameobject 0 Answers

rotate and tilt at the same time 1 Answer

How can i follow an object by 1-axis-rotation? 0 Answers

Another way to follow an object? 2 Answers

LookAt inaccurate rotation issue (javascript) 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