Problem with Rotation - AI

Hey so I need help with some AI for a dragon that will not rotate correctly upwards in the sky toward a target object. I have been moving land characters around with the code below which basically rotates and moves the characters toward the target object. The problem occurs when I move the target object up into the sky, the dragon does not rotate upwards when it travels toward it’s target. It rotates and travels toward the target, but it does not rotate upwards which is it’s X axis. How can I make this code also rotate the dragon upwards toward the target? I dont understand why the dragon rotates to look at the target, but not in all axis?

// Variables
var moveSpeed     = 3; //move speed
var rotationSpeed = 5; //speed of turning
var target  : Transform; //the dragon's target
var target2 : Transform;
var range            : float = 10f;
var range2           : float = 10f;
var stop             : float = 3f;

    // Move and rotate
    var distance = Vector3.Distance(myTransform.position, target.position);
  
    // ROTATE to look at the target
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    // MOVE towards the target
    if (distance <= range && distance > stop && dead == false)
    {
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
        Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
        GetComponent.<Animation>().CrossFade("fly", .12);
    }
    // STOP
    if (distance <= stop)
    {
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
        Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    }
}

you check the look rotation, but what if the dragon is upside down? he still could look towards the target, but it would not rotate itself, you have to consider this.

1 Like

There isn’t anything in your code that keeps it from pointing up. You could check it with a simple cube or something with a sphere parented to the front. I’m not sure what’s doing it. It just subtracts the two positions and looks in that direction over time because of the slerp. Maybe increasing rotationspeed would help if it’s taking too long to turn. If you have a rigidbody, maybe one axis is frozen.

1 Like

I remember looking at this code yesterday. I wasn’t sure what was preventing it from looking, either.

Make sure something else is not interfering – checking with a cube or something is a good idea, too, as then you can be sure it’s the code and not something else pretty quickly.

A frozen axis on a rigid body won’t prevent direct position or rotation movement, by the way. That will only prevent physics simulation changes.

Last, but not least… I would suggest that you try to convert your scripts to C# going forward and abandon UnityScript. :slight_smile:

1 Like

Thanks for looking it over guys, I am going to try this out with a cube and see how it goes. I’ll post back. Also, @methos5k , I agree I need to switch to C# :slight_smile:

Sigh…Works perfectly with the cube…the dragon has pretty complicated prefab setup, might be a rigidbody on one of the other child objects messing things up. I’ll put up a complete answer when I figure it out if anybody runs into something like this. Post back soon :slight_smile:

So I got it working! The dragon has several primitive colliders that makeup the mesh’s collision, 6 in total. None of them have rigidbodies, only the parent object with the movement script has a rigidbody. For some reason that I do not understand, disabling the two leg colliders fixes the weird rotation issue, and also fixes an even more severe problem where the dragon would fly way above each target waypoint and get stuck. (I made a system where there are a bunch of waypoints/triggers and basically the dragon collides into them and it sends it to the next waypoint) I dont understand why the other, seemingly identical colliders do not cause the problem when enabled. If anybody has any ideas I would love to have more knowledge of what could be throwing off the calculation, or if my setup is simply not done correctly? Thanks!

I can’t think of anything atm, but I’m glad you got it mostly resolved. :slight_smile: