Making an object rotate to face another

I want my object to rotate to face a moving target object BUT I want it to turn slowly towards the target with a lag. Like a huge rock rotating, it isn't going to happen instantly.

transform.LookAt doesn't work because it obviously 'snaps' to the target, and I can't figure out how to use RotateTowards. I even tried modifying eulerAngles, and so on but I was out to lunch. The only useful thing I can do with vector math right now is get a vector that points from one object to another by subtracting one from the other. Wow, skill.

If someone could point me in the right direction it would be great, I have a pretty good idea of what needs to happen, but no clue on implementing it with Unity.

You want to be framerate-independant, so how much you lerp will be the 'strength' property multiplied by the time of the last frame. Thing is: if you have a very strong strength a very slow machine, you might end up with a lerp factor that is greater than one, causing overshoot. This line figures out how much you want to lerp, and makes sure that doesn't happen.

EDIT: Oh, and you might want to rename Update to LateUpdate. This will make sure the rotation gets fixed just before rendering, so you don't risk that you look at where the object is before the object moves - resulting in a 1-frame lag.

Thanks a lot for taking the time to explain that to me Nicholas. I think I am understanding it now, and I googled Lerp to find out what it does. I just figured out I will have to use similar concepts in the next part of my script so this is very good.

Thank you very much for your time, this makes me very happy as I have been working for quite a while as it's 6:00am here.

I am working on learning AI in Unity and this code was for Steering behaviours. I will put my finished code on the wiki later as someone may get some use out of it. What I have already is good for a space game.

I may even get to writing flocking code and other group behaviours eventually

So, to carry this out to another direction (no pun intended.) Can you "average" quaternions? I'm working on a flocking behaviour for some of my game objects, and I'm curious if I can take the raw rotations and average several of them and still come out with a legit rotation. Can quaternions be divided?

So, to carry this out to another direction (no pun intended.) Can you "average" quaternions? I'm working on a flocking behaviour for some of my game objects, and I'm curious if I can take the raw rotations and average several of them and still come out with a legit rotation. Can quaternions be divided?

Click to expand...

You would probably want to use euler angles for that to simplify things as euler angles are Vector3's and quats have 4 elements (x,y,z,w). You can do just about everything to a Vector3. Take a look at the docs for the Vector3 class and Quaternion.ToEulerAngles().

For the steering behaviour stuff I did, I used Vectors to calculate the steering force for whatever behaviour, and I had a seperate locomotion function to apply the force to my agent. That way for your flocking you can calculate the behaviours needed for flocking (alignment, seperation, cohesion, etc), mix the behaviours with whatever method, and do your movement seperately.

I haven't done flocking just yet, The main hurdle seems to be figuring a way to partition the game world for speed. Just out of curiosity, how many objects are going to be flocking? What type of behaviour(s) are you going to implement? I would really be interested to hear how you get this going.

My gut instinct was to use Euler angles, but what happens when the Boid I'm currently examining for an update is heading due north (0°), and hir neighbors are aimed slightly to the left and right... 355° and 5° respectively. When I average those, I'll end up with the boid wanting to steer due south (180°) which clearly isn't what it should be doing (for the Alignment component of the behaviour)

Is there some clever trick I'm overlooking? Adding some value or doing some RMS kind of thing... or are Euler angles just shot?

Oh, and it might be helpful to point out that it's going to be a herd of cattle, so the behaviour is essentially "in the plane" of the ground (So only the Euler[1] really matters, as far as I can tell)

It is really nice that you are just working on a flat plane so you only have to worry about the x and z components of the vector.

I still think you can do this with euler angles...

The way I learned to do flocking (have never written the code just learned it) is you calulate the three behaviours I mentioned (alignment, seperation, cohesion) and then through a method to combine them (prioritized dithering, etc) you calculate the steering force Vector (direction and force) of each boid.

But to script cows, you could probaly go a LOT simpler. For example, are they going to be used to fill an area and basically eat and wander around, or are they going to be running? If they are just in a field, you could just use a wander behaviour for each cow and you check that your cow stays within a distance from the "lead" cow. When it get too far away from the lead, it will turn towards lead and move towards it (arrive behaviour) and then resume wander once it is back in the radius.

Actually, I think you could get away with using my suggestion above for running cows as well with a few adjustments. You would want to mix the Arrive behaviour with the seperation behaviour, cohesion would already be taken care of because of arrive, and that way when your lead cow starts running, all of the other follow suit. Then when they are stopped, the wander behaviour kicks in automatically.

Unity Technologies

Basically yes you can. There is one thing you need to watch out for called the double cover operator, it means quaternions have 720 degrees of rotation.

Thus you need to adjust for it.
You can check if two quaternions are more than 360 degrees arpart using
Quaternion.Dot (a, b) < 0

To make a quaternion go to the right side of the 720 degrees you multiply all components by -1.

So yes it is possible and it is not completely straightforward too. Thats why we haven't exposed all math operators to our quaternion class. Except for multiplying rotations and Lerp/Slerp it is not really intuitive what you can do with quaternions and what not.

If you just use Slerp and multiplication the dirty stuff will be handled for you.