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 Goody! · Dec 22, 2009 at 12:24 AM · aipath

Make a drunk character swerve and loop on the way to a waypoint but still make it eventually.

Hello, and thanks in advance for you consideration.

I have rigid body game objects that are going to be pathing to defined triggers "goals" in the world. I'm probably going to use constant force on them to move them as I have to pay the price of having them as rigid bodies anyways.

I think I can figure out how to set the constant force to get them straight to their goals but I'd like them to loop and veer on the way there, kinda like a really drunk guy walking down the middle of the street. I don't need traditional pathfinding since there will be nothing in the way they need to navigate around.

EDIT: FYI the goals may be moving, not very far, but at varying speeds.

Can anyone suggest a method for doing this?

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 Goody! · Dec 24, 2009 at 05:45 AM 0
Share

Thanks for the answers and I'm sorry I haven't responded, my initial enemy code has yet to be at a stable enough state to test these out. Once I get one going I'll post the results back here.

avatar image Goody! · Jan 04, 2010 at 01:05 AM 0
Share

Enemies and hazard objects still... not... there... but, these answers haven't been forgotten!

2 Replies

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

Answer by duck · Dec 22, 2009 at 07:58 AM

Whichever way you do this, you probably want to use a gradually changing set of values rather than affecting the movement by random numbers each frame, as this will usually result in random jittery movement as opposed to smooth loops and curls. Both my examples below use the Sin function to give this kind of smoothly changing movement.

Also I'm assuming you only want to modulate the movement in the x-z plane as is usual with many games.

One way you could approach this is to modulate the target position that your 'drunk guy' is heading for. For example, in FixedUpdate, you could calculate a dummy target position which is offset from your real target position by an amount proportional to the current distance from the target. This will give you quite a uniform left-to-right swerving which reduces as it approaches the target.

// adjust these to taste: float swerveAmount = 0.5f; float swerveSpeed = 2;

void FixedUpdate() { Vector3 delta = target - transform.position; Vector3 deltaP = new Vector3(-delta.y,0,delta.x); Vector3 offset = deltaP swerveAmount Mathf.Sin(Time.time * swerveSpeed); Vector3 swerveTarget = target + deltaP;

 // then apply forces to head towards 'swerveTarget'!

}


Another method which would give you more random looping and curling might be to use two objects:

1 - A hidden dummy object which moves straight for the target, as you've suggested that you can already do (if you want to use forces, this could be a rigidbody with no collider or renderer). We'll call this the 'seeker'.

2 - A separate 'drunk guy' rigidbody which is visible, which always heads for a point which is offset from the 'seeker' object by a varying amount.

For example, on the 'drunk guy' object, you would have something like this:

// adjust these to taste: float swerveAmount = 3f; float swerveSpeed = 2;

// distance from target at which swerving starts to reduce float thresholdDist = 20;

void FixedUpdate() { float dist = (target.position - seeker.position).magnitude; float reduce = Mathf.Clamp01(dist / thresholdDist); float offsetX = Mathf.Sin(Time.time swerveSpeed) swerveAmount reduce; float offsetZ = Mathf.Cos(Time.time swerveSpeed) swerveAmount reduce; Vector3 offset = new Vector3(offsetX, 0, offsetZ); Vector3 swerveTarget = seeker.position + offset;

 // then apply forces to head towards 'swerveTarget'!

}

(these are illustrative untested code snippets. if you encounter errors, please leave a comment and I'll fix them!)

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 runevision ♦♦ · Dec 22, 2009 at 09:36 AM 0
Share

To get something a little less regular than sin and cos, Perlin noise could be used ins$$anonymous$$d. See http://unity3d.com/support/resources/example-projects/procedural-examples

avatar image
2

Answer by Peter Alexander · Dec 22, 2009 at 02:02 AM

You could apply a random force that diminishes as he gets closer.

float DiminishingForce(float distance)
{
    return Mathf.Clamp(0.0f, MAX_FORCE, distance * FORCE_RATIO);
}

Just multiply your random force by that. Of course, you'll need to define MAX_FORCE and FORCE_RATIO.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Attack Script problem please help 1 Answer

A* Pathfinding, destroying a path once it has reached its end 0 Answers

Waypoint Setup: How? 4 Answers

unitysteer and path 1 Answer

Delete Astar Path gride graph around a particular Object 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