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;

        // move into the desired direction
  • 2
    \$\begingroup\$ 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... \$\endgroup\$ – Mikael Högström Jul 19 '12 at 16:32
  • \$\begingroup\$ 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 \$\endgroup\$ – Darren Jul 19 '12 at 16:37
  • \$\begingroup\$ Don't you just want it to look in the walk-direction? As in LookAt(walk)? \$\endgroup\$ – Mikael Högström Jul 20 '12 at 8:47
  • \$\begingroup\$ no LookAt uses targets, and walk isn't a target it's actually just to get the distance between objects \$\endgroup\$ – Darren Jul 20 '12 at 8:56

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
| improve this answer | |
  • \$\begingroup\$ +1 Good link! Arrival is harder than one would expect :) \$\endgroup\$ – Mikael Högström Jul 23 '12 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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