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 Dreave · Nov 18, 2011 at 04:47 PM · aienemyfollow

Enemy Follow Script Help

How would I change this script so that when the enemy gets lets say 1 metre in front of the player it stops and that if the player jumps the enemy dosent glitch out?

 var target : Transform; 
 var moveSpeed = 3; 
 var rotationSpeed = 3; 
 var myTransform : Transform; 
 
 function Awake(){
     myTransform = transform; 
 }

 function Start(){
      target = GameObject.FindWithTag("Player").transform; 
 }
 
 function Update () {
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
  
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }

 

P.s I didnt write this script all thanks goes to Orangelightning.

Comment
Add comment · Show 2
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 FLASHDENMARK · Nov 18, 2011 at 05:06 PM 0
Share

That must be ancient code because I really don't remember writing that, but thanks for the credits :D

EDIT: By "glitching out" do you mean that the enemys rotation gets all weird?

P.s I reformatted you code so it is much easier to read now.

avatar image Dreave · Nov 18, 2011 at 06:39 PM 0
Share

You answered a question with this script here http://answers.unity3d.com/questions/33107/enemy-ai-script.html

and yeah when I jump if the enemy is close enough it spins round and sometimes falls over.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Nov 20, 2011 at 02:18 PM

You can consider only the horizontal direction - calculate the direction, then zero the y component to make the direction vector completely horizontal:

function Update () {
    var lookDir = target.position - myTransform.position;
    lookDir.y = 0; // zero the height difference
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
Comment
Add comment · Show 5 · 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 Dreave · Nov 20, 2011 at 05:46 PM 0
Share

That's perfect thanks!

avatar image MP0732 · Nov 24, 2012 at 06:33 AM 0
Share

Thanks a million, man. I've been searching around the Internet for almost three days looking for a solution to this problem. I just wasn't looking up the right search ter$$anonymous$$ology.

avatar image tamsii · Aug 04, 2014 at 10:14 AM 0
Share

can someone translate the script in c#?

avatar image meat5000 ♦ · Aug 04, 2014 at 10:17 AM 0
Share

$$anonymous$$aybe a good opportunity to try something like this out

avatar image tamsii · Aug 04, 2014 at 10:25 AM 0
Share

is lookDir Vector3?

avatar image
0

Answer by XYZglitch · Aug 16, 2014 at 12:34 PM

,THIS WORKED THANK YOU SO MUCH! Been looking everywhere!

Comment
Add comment · Show 1 · 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 Calum1015 · Apr 28, 2015 at 01:38 AM 0
Share

Don't leave at moment as an answer please. :)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Stay Back! 2 Answers

Shooting Damage Help 1 Answer

Need help with enemy AI 1 Answer

How do I make a enemy follow me 3 Answers

AI enemy scripting help 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