• 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
Question by SirGive · Dec 13, 2012 at 01:50 AM · movementai

Game NPC "Guide" Movement (Much like Navi from Zelda)

What I'd like to accomplish is a fluid, seemingly (or maybe actually) random movement from the guide object around the the height of the player's head. Something very similar to Ocarina of Time's Navi. Her flight patterns seem very procedural, something I would like to do as well.

I can think of a couple of ways to tackle this, but none of which seem elegant:

  • Animation based "up and down" and lerp between points (creating the bobbing effect while moving)

  • Basing waypoint distance values at 'x' amount from the player, and making the movement lerp both towards the waypoint and up and down

So, what would be a good way to approach this? I feel that there is a good formula out there that could accomplish a float-y up and down movement.

For the random distance movement, I'm leaning towards randomly choosing a point within a certain distance of the player and at a certain range away from the previous position. All points being relative to the player, of course.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by SirGive · Apr 08, 2013 at 11:45 PM

So the solution to this is very very simple, and is broken down into the parts I've outlined above. The one thing I tend to neglect is utilizing trig functions!

For this particular effect, a sine wave works perfectly: alt text

This is the curve in which the bobbing up and down will work. We'll be working in one plane - zy. Here's an example of that (with the no clear flag turned off on the camera so you can see the path):

alt text

So here's some variables we'll need to define:

 /// <summary>
 /// Average value, offset from zero. 
 /// This is the point at which the object will float around (on the y - axis)
 /// </summary>
 public float z = 0;
 
 /// <summary>
 /// Amplitude, this is how high and low the curve will go
 /// </summary>
 public float a = 1;
 
 /// <summary>
 /// Angular Frequency, this is how fast it will traverse the curve
 /// </summary>
 public float b = 3f;
 
 /// <summary>
 /// Phase Angle, check back for more details *I still don't really know myself*
 /// </summary>
 public float c = .5f;

 /// <summary>
 /// this is how far the curve has been traversed
 /// </summary>
 public float x = .01f;

These will all work in our sine wave function: y = z+ a * sin(bx + c) Which we will apply in code:

     /// <summary>
     /// Late Update to smooth out our movement. This is very rough.
     /// The first step is move to the target, and then to bob up and down
     /// The last step to to move forward on the wave by increasing the x
     /// </summary>
     void LateUpdate () 
     {
 
         MoveToPoint();
         //y = z+ a * sin(bx + c)
         this.transform.localPosition = new Vector3(this.transform.localPosition.x, 
                                     (z + a * Mathf.Sin(b*x + c)),
                                     this.transform.localPosition.z);
         x += Time.smoothDeltaTime;
     }

It is VERY IMPORTANT TO NOTE that the way this script works is to be on an sub object. So the hierarchy would be like this:

alt text

And the movement code would be as follows:

 public void MoveToPoint ()
     {
         if (target == null)
             return;
         transform.parent.position = Vector3.Slerp(transform.parent.position, target.position+Vector3.up*2, speed);
     }

This script will work, but are some important improvements to make - combining the sine wave formula with the follow formula. This is a good base, so good luck!


wave.png (129.4 kB)
inspector.png (29.5 kB)
Comment
SrBilyon
Unitraxx

People who like this

2 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Controlling AI Movement 0 Answers

Do something every each multiple of 1000.. 2 Answers

2D Random AI 1 Answer

What is the Code Of AI ball rolling to the target C# thanks 1 Answer

2D Game, i need to make an AI for the enemy to walk towards the player without rotating 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