Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using xaitment plugin for Unity as the AI for a demo level but I have a problem. My character is supposed to walk to random points but while it's walking there it's rotating around the point. I can't see what is causing this in my code, I'm assuming it's something to do with my lookat function

using UnityEngine;
using System.Collections;

public class MovementGoto : MonoBehaviour {

    // target position for the movement
    private Vector3 mTargetPosition;
    public Vector3 TargetPosition
    {
        get { return mTargetPosition;}
        set { mTargetPosition = value;}
    }

    public float Speed = 40.0f;

    // Update is called once per frame
    void FixedUpdate() 
    {
        // get the current steering direction to reach the target position
        Vector3 walk = this.mTargetPosition - this.transform.position;

        // ignore 3d
        walk.y = 0.0f;
        walk.Normalize();

        // move into the desired direction
        this.transform.LookAt(this.TargetPosition);
        this.transform.Translate(walk); 
    }
}
share|improve this question
2  
Where do you want the character to look at? I've never used unity but the LookAt method most often either takes a direction or two positions and calculates the direction between them. Seems like you send one position (not a direction) into the method and I guess that could be the source of your problem but I could also have misunderstood your question... –  Mikael Högström Jul 19 '12 at 16:32
    
that could be it actually, I thought the lookat function took positions as in telling it to look at this position, ok I'll just have to work out the angle it has to turn to face the point it's walking to –  Darren Jul 19 '12 at 16:37
    
Don't you just want it to look in the walk-direction? As in LookAt(walk)? –  Mikael Högström Jul 20 '12 at 8:47
    
no LookAt uses targets, and walk isn't a target it's actually just to get the distance between objects –  Darren Jul 20 '12 at 8:56

1 Answer 1

I think you may be using the lookAt function incorrectly if you're getting arcs from it. You're using steering, so you may need to overcome any initial velocity. Check out the red3d.com article on steering, particularly arrival.

From the article, arrival should look like:

target_offset = target - position
distance = length (target_offset)
ramped_speed = max_speed * (distance / slowing_distance)
clipped_speed = minimum (ramped_speed, max_speed)
desired_velocity = (clipped_speed / distance) * target_offset
steering = desired_velocity - velocity
share|improve this answer
    
+1 Good link! Arrival is harder than one would expect :) –  Mikael Högström Jul 23 '12 at 13:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.