Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Xatoku · Jun 26, 2011 at 07:04 PM · playerenemyfollowjitter

Enemy Jitters When Following

I've got my enemy to look at and chase the Player, but once he gets to the distance where he should stop chasing the Player, he'll do tiny nudges towards the Player, getting closer to him. Anyone have a clue why this is happening?

 function Update(){
 
 //FIND THE PLAYER & CHANGE STANCES//
     if(!FindPlayer){
         Player = GameObject.FindWithTag("Player");
         FindPlayer = true;
     }
     var distanceToTarget = Vector3.Distance(transform.position, Player.transform.position);
     if(distanceToTarget <= 3){
         targeted = false;
     }
     if(distanceToTarget <= 15 && distanceToTarget > 3){
         StanceChange = gameObject.GetComponent("EnemyControl");
         StanceChange.BattleStance = true;
         targeted = true;
     }
     if(distanceToTarget > 15){
         StanceChange = gameObject.GetComponent("EnemyControl");
         StanceChange.BattleStance = false;
     }
 //LOOK AT THE PLAYER ONCE TARGETED//
     if(targeted){ 
         var rotation = Quaternion.LookRotation(Player.transform.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
         transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);
         ChasePlayer = gameObject.GetComponent("EnemyControl");
         
 //CHASE THE PLAYER//
         animation.CrossFade("run",0.2);
         var controller2 : CharacterController = GetComponent(CharacterController);    
         var forward : Vector3 = transform.TransformDirection(Vector3.forward);
         controller2.SimpleMove(forward * walkSpeed);
     }
 }
Comment
Add comment · Show 5
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 Dreamblur · Jun 26, 2011 at 08:38 PM 0
Share

You might want to set some Debug.Log messages inside your loops to check if your values are actually being set to what they need to be.

If I have to guess though (and assu$$anonymous$$g that no other script is interfering with that script), I'd have to say that your enemy never gets within stopping range of the player and that your Quaternion.Slerp is at fault. You see, you're slerping using Time.deltaTime, which means that unless your damping variable has a relatively high value, then your enemy never actually gets to face the exact direction as your player. It would always stop just a small angle from the actual direction vector and would keep readjusting itself as long as it's being called. Couple that with the forward movement that's being called afterwards and you're possibly causing a scenario where the enemy is moving in a small circular motion near the range while not reaching the range while continuously slerping towards the ever-changing target rotation.

Anyway, while my theory may seem plausible, it can't be verified unless you actually set Debug.Log messages like I suggested.

avatar image BerggreenDK · Jun 26, 2011 at 11:28 PM 0
Share

@Jordan 9: I have removed my answer, since you seem to be more keen on voteing down than getting my help. BYE!

avatar image Waz · Jun 26, 2011 at 11:41 PM 0
Share

Your above code is fine (ignoring inefficiencies), so you need to look and show us elsewhere.

avatar image Xatoku · Jun 27, 2011 at 01:07 AM 0
Share

Well I think that it's switching the targeted variable on and off but it should just stay off, if that helps at all :S.

avatar image Dreamblur · Jun 27, 2011 at 02:22 AM 0
Share

I tried your code and it does what it's supposed to do. The enemy, assu$$anonymous$$g that the required values are all tweaked to scale, is able to to get to within stopping range and stops when it does. No values are fluctuating or anything, so the problem really is in some other script.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Enemy backing away from player after going around corner? 1 Answer

Enemy is not stopping following the player 2 Answers

Enemy reorienting for player 1 Answer

Camera rotation around player while following. 6 Answers

Enemy to follow the Player 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