Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by chrisandress · Mar 16, 2014 at 03:43 AM · followenemy aitilt

My following enemy AI tilt back as they get near.

I am making a game while I learn and I have a simple script where AI "zombies" spawn and make their way towards me. Everything is working just how it should so far except as they get close to me they begin to tilt backwards until they are flat once they reach me. This is the code I am using for the AI. Can anyone tell me why they are tilting backwards because I am stumped so far.

     public Transform target;
     public float moveSpeed;
     public float rotationSpeed;
     private float distance;

 private Transform myTransform;

 void Awake ()
 {
     myTransform = transform;
 }
 // Use this for initialization
 void Start () 
 {
     GameObject follow = GameObject.FindGameObjectWithTag ("Player");

     target = follow.transform;

     distance = 1.5f;
 }
 
 // Update is called once per frame
 void Update () 
 {
     Debug.DrawLine (target.position, myTransform.position);

     //Look at target
     myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);

     if (Vector3.Distance (target.position, myTransform.position) > distance) 
     {        
         //Move towards target
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
 }
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
Best Answer

Answer by robertbu · Mar 16, 2014 at 03:47 AM

My guess is that your enemies are shorter than your player, or at least the pivot point of your enemies is below the level of the pivot point on the player. You can fix it by replacing line 28 with:

 Vector3 dir = target.position - myTransform.position;
 dir.y = 0.0;
 myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);

This removed the 'y' component of the direction to look, so your object will not look up or down. This code assumes that your game is setup so that your objects are moving on the XZ plane.

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 chrisandress · Mar 16, 2014 at 04:28 AM 0
Share

That fixed it. I actually found that solution while looking earlier. It didn't work then. Not sure what changed, but thank you.

avatar image AshishNikhil · Sep 06, 2021 at 08:34 AM 0
Share

Thanks, You have a brilliant $$anonymous$$d. May God bless u!

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

22 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

Related Questions

Enemys dont collide in follow script 1 Answer

Camera rotation around player while following. 6 Answers

How do I make a enemy follow me 3 Answers

I changed my monitor resolution and now my enemies are chasing me too fast ?? 2 Answers

How do I keep enemies from overlapping 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