Hey guys, 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. Thanks guys, -Jeremy

try: Code (csharp): var target : Transform; var strength = .5; function Update () { targetRotation = Quaternion.LookRotation (target.position - transform.position); str = Mathf.Min (strength * Time.deltaTime, 1); transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str); }

Wow, certainly beyond me. Thanks a lot Nicholas, I will try this tomorrow and try to figure out how it all works. I am fine up until the Math.Min and the Lerp. Thanks a lot, -Jeremy

The trick is to remember that a Quaternion is a rotation. This line figures out the rotation from this object to the target Code (csharp): targetRotation = Quaternion.LookRotation (target.position - transform.position); This line Lerps the rotation from where we're looking now towards the object. 'str' is how much we lerp. Code (csharp): transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str); 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. Code (csharp): str = Mathf.Min (strength * Time.deltaTime, 1); 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. -Jeremy

UPDATE: All is working perfectly. Thanks. 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 -Jeremy

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?

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. Hope that helps a bit, -Jeremy

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 would really help to figure it out if you posted a code snippet. 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. If they are running, then it will be harder. -Jeremy

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. -Jeremy

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. Code (csharp): var Quaternion[] quats; Quaternion avg = Quaternion(0,0,0,0); for (var q : Quaternion in quats) { if (Quaternion.Dot (q, avg) > 0) { avg.x += q.x; avg.y += q.y ; avg.z += q.z; avg.w += q.w; } else { avg.x += -q.x; avg.y += -q.y; avg.z += -q.z; avg.w += -q.w; } } var mag = Mathf.Sqrt(avg.x* avg.x + avg.y* avg.y + avg.z * avg.z + avg.w * avg.w); if (mag > 0.0001) { avg.x /= mag; avg.y /= mag; avg.z /= mag; avg.w /= mag; } else avg = quats[0]; Edit: I edited the code not to multiply by weights

Holy crap. This is the part where I wish I'd taken that Linear Algebra class in school. But, it does make sense conceptually (the averaging, not necessarily the multiplication) Once again, Joe, you've proven that you are, without a doubt The Man

Wait... perhaps excuse my early morning stupidity... but what language is that snippet in? It looks like a sort of blend of C# and JavaScript. Or is it more of a "Forum Pseudocode" kind of thing? Or have I just been stuck in PHP land for so long that my language skills are eroding?

It's supposed to be javascript but i didn't try it before posting it. Actually the weight multiplication is kind of weird and not necessary since we renormalize the quaternion at the end anyway.