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 87morpheus · Jun 05, 2013 at 06:46 AM · c#animationenemy aimechanimunity4.0

Enemy AI Script Glitch

Hello Unity Community, I am here to ask for some help on my Enemy AI script. I am trying to figure out a way to make a script where I can use the new Mecanim system that was introduced in Unity 4.0. My enemy seems to have a tough time deciding when to run left, right, or forward. Here is the part of the script that I am having trouble with...

 var distance = (target.position - myTransform.position).magnitude;
         Vector3 dir = (target.transform.position - transform.position).normalized;
         float direction = Vector3.Dot(dir, transform.forward);
         float dirRightLeft = Vector3.Dot(dir, transform.right);
 
     animator.SetFloat("Speed", movingSpeed);
             
             if(direction > 0.8f && dirRightLeft < 0.2f)
                 animator.SetFloat("Direction", curDir = runForward);
             
             if(direction < 0.8f && dirRightLeft > 0)
                 animator.SetFloat("Direction", curDir - 0.1f * Time.deltaTime);
             
             if(direction < 0.8f && dirRightLeft < 0)
                 animator.SetFloat("Direction", curDir + 0.1f * Time.deltaTime);
             
             if(curDir > 1f)
                 curDir = 1f;
             if(curDir < 0)
                 curDir = 0;
                 
                 
             //look at target
 //            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, 
 //            Quaternion.LookRotation(target.position - myTransform.position), rotSpeed * Time.deltaTime);
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 Proportion1 · Jun 05, 2013 at 07:33 AM 0
Share

what is the animation that the "Direction" float is meant to activate? a screenshot of your blendtree showing what direction is meant to do would be good. also you should set how the value of curDir is changed outside the SetFloat function, because it doesnt make much sense what curDir is mean to do there.

avatar image 87morpheus · Jun 05, 2013 at 07:36 PM 0
Share

Of course, I didn't think about giving you a screenshot of the blendtree. Here it is, as you can see curDir decides which direction to run. The closer it gets to 1, the more he runs to the right, the closer it gets to 0, the more he runs to the left, and when it get's to 0.5, he runs forward.

alt text

blendtree.png (36.3 kB)
avatar image Proportion1 · Jun 05, 2013 at 09:57 PM 0
Share

ya i can see what your doing wrong, but before i can answer i need to know what the 2 variables in your if condition are meant to do (direction and dirRightLeft)

avatar image 87morpheus · Jun 06, 2013 at 06:14 AM 0
Share

Okay I edited the script in the answer to give the information you need.

avatar image 87morpheus · Jun 12, 2013 at 04:33 AM 0
Share

I was doing some stuff to study a little more on scripting and I think using $$anonymous$$athf might help. I'll be doing some research on it tomorrow because I am a little busy with something tonight, but if you have any advice or anything, please let me know.

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

15 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

Related Questions

Changing animation update mode through code 2 Answers

(Mecanim) Using a Variable for Transition Offset (Edited) 0 Answers

The problem of Enemy scale when he follow the player (script) . How to fix? 0 Answers

Multiple Cars not working 1 Answer

two animations interacting smoothly 0 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