x


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?

more ▼

asked Dec 22 '09 at 12:24 AM

Goody! gravatar image

Goody!
516 24 31 46

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.

Dec 24 '09 at 05:45 AM Goody!

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

Jan 04 '10 at 01:05 AM Goody!
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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!)

more ▼

answered Dec 22 '09 at 07:58 AM

duck gravatar image

duck ♦♦
41k 92 148 415

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

Dec 22 '09 at 09:36 AM runevision ♦♦
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 22 '09 at 02:02 AM

Peter Alexander gravatar image

Peter Alexander
931 5 6 23

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x959
x238

asked: Dec 22 '09 at 12:24 AM

Seen: 1523 times

Last Updated: Feb 17 '10 at 12:53 PM