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 DevMerlin · Feb 18, 2013 at 10:20 PM · aicharactercontrollerslerp

Can you help me make this code turn smoothly?

I have two states in my Follower script - random wander, and following the player. When it wanders at random, it jerks at sharp angles to make turns. However, following the player it moves smoothly from point to point. I want to reconcile the two effects and have the wandering rotate smoothly as well.

         direction.y = 0;
         int speed = walkSpeed;
 
         if (walkInRandomDirection) {
             speed = randomSpeed;
         }
 
         if (executeBufferState) {
             speed = runSpeed;
         }
 
 
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
         transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
 
         Vector3 forward = transform.TransformDirection(Vector3.forward);
         float speedModifier = Vector3.Dot(forward, direction.normalized);
         speedModifier = Mathf.Clamp01(speedModifier);
 
         direction = forward * speed * speedModifier;
         characterController.Move(direction * 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 robertbu · Feb 18, 2013 at 10:51 PM 0
Share

Please post the code you use to generate the directions for both random walking and when following the player. I don't think the problem is in this code.

avatar image DevMerlin · Feb 18, 2013 at 10:57 PM 0
Share

Sure, although I haven't written in the following code yet. That came from a previous iteration that did not have random walking.

This is based off Ezzerland's simple AI behavior script, the full version of which is here: link text

     void WalkNewPath() {
         if (!walkInRandomDirection) {
             walkInRandomDirection = true;
             if (!playerHasBeenSeen) {
                 randomDirection = new Vector3(Random.Range(-0.15f,0.15f),0,Random.Range(-0.15f,0.15f));
             } else {
                 randomDirection = new Vector3(Random.Range(-0.5f,0.5f),0,Random.Range(-0.5f,0.5f));
             }
             randomDirectionTimer = Time.time;
         } else if (walkInRandomDirection) {
             $$anonymous$$oveTowards (randomDirection);
         }
         
         if ((Time.time - randomDirectionTimer) > 4) {
             //choose a new random direction after 2 seconds
             walkInRandomDirection = false;
         }
 
     }


avatar image Benproductions1 · Feb 18, 2013 at 10:59 PM 0
Share

Could you describe the Jering a bit more? It seems that randomising the speed might be a problem...

avatar image DevMerlin · Feb 18, 2013 at 11:03 PM 0
Share

I could probably do without that part, really. Ezzerland developed it to be an enemy AI script, I'm converting parts like this for a different type. - Okay.... did some checking and that's not the cause, all it does is ease forward.

avatar image robertbu · Feb 18, 2013 at 11:52 PM 0
Share

There are some things here that are a bit strange to me and that I don't fully understand. I don't have time to flesh it out and put the scripts into Unity, so I'm going to make a couple of observations that may help.

It looks like in this line...

 randomDirection = new Vector3(Random.Range(-0.5f,0.5f),0,Random.Range(-0.5f,0.5f));

...that the new random direction could be any direction. $$anonymous$$aybe what you want is more turns but only allow a limited change in direction from the current forward direction.

And in this line...

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);


...this use of Slerp() will cause faster rotations for larger angles. You might experiment with Quaternion.RotateTowards() ins$$anonymous$$d.

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

10 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

Related Questions

Should you use CharacterController.Move or SimpleMove for enemies? 1 Answer

How to make AICharacterController able to walk on walls and ceilings? 0 Answers

Keeping Character From Walking Through Walls 1 Answer

Monster doesn't move 0 Answers

Small Project, tutorials/help needed 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