Fly towards

I have a flocking algorthm with a lot of units flying together. This works pretty well but the leader part does not work good. If I remove all the flocking from the leader and do this:

 Vector3 towardsTarget = (targetPosition-position);
towardsTarget.Normalize();
towardsTarget *= leaderFroce;
steeringForce += towardsTarget;

// Amount of speed should have a max
if(steeringForce.magnitude > maxForce)
		{
			steeringForce.Normalize();
			steeringForce = steeringForce * maxForce;
		}

// Turn velocity into movement
velocity += steeringForce * Time.deltaTime;
		
// Update location
transform.position += velocity * Time.deltaTime;

// Here the problem lies:
transform.rotation = Quaternion.LookRotation(velocity);

The last line of code is to make the unit look towards its direction of flying. It does also but at the same time it messes with the direction of which the unit flies. It will go past the target but not directly through it. Any thoughts?

Be careful with the transforms, transform.position + transform.rotate have to have a capital for Position and Rotate

?
Edit:

Also. I will get a problem if I have a velocity at the start. Then it just wont work :(.

// Update location
transform.position += velocity * Time.deltaTime;

// Here the problem lies:
transform.rotation = Quaternion.LookRotation(velocity);

position and rotation must be spelt with capital letter (Position and Rotation)

no such thing as transform.Rotation

the code compiles…

I use it in many of the scripts I have ?

His use in the above script is correct. lowercase is used.

@OP:
Sounds like you’re having a similar problem as this user:
http://forum.unity3d.com/threads/91036-Need-Alternative-AI-Script

Basically that you need to clamp some of your angles to prevent your AI from turning itself around as it gets closer to the target. Sound about right?

No, the thing is that the unit takes a very wide loop and misses the target.

http://postimage.org/image/uovbeg04/

Left is what happens and right is what should happen.

This is the initialization(which is probably causing the issue):

velocity = transform.forward * 5 + Random.insideUnitSphere;
		velocity.Normalize();
		velocity *= maxForce;

I can’t see the pictures from here (I’m at work atm).

What I mean is, it sounds like the issue is in your rotation. That your flock rotates improperly and when it gets too close it will begin to rotate around the target instead of into the target.

Granted, I’m not in a place where I can test this, but that would be my first guess. Try changing your rotation to something similar to the one in that thread. In this case, you’ll need to generate a rotation speed variable (20 is probably fine)

public float rotationSpeed = 20.0f;

That’s in c# ofc so you might need to convert to js.

Then change your rotation technique from:

transform.rotation = Quaternion.LookRotation(velocity);

to

		transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(velocity), rotationSpeed * Time.deltaTime);
		transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);

also, and this may be just a matter of preference, but, I’d perform the rotation before I’d perform the movement. You might even try that before changing things around.

I’ll take a closer look later on when I have the ability to. Right now I’m pretty sure your problem is in the above so I’ll stick with that.

Hi. Thank you for the info. The problem however is sort of found. I set a velocity at the beginning. And afterwards I just use a steeringForce which never fully out-powers the original velocity. I guess I will have to use steeringForces for all my movements and not set the velocity directly.

The thing is that I want to switch between flocking-behaviours and other behaviours(like chasing an enemy, doing loops etc). If anyone now of a good source for ideas on how to implement this(basicly squad flight-behaviours)

http://forum.unity3d.com/threads/89543-Free-Simple-AI-Behavioral-script

Some idea’s of what you mentioned can be found in the above code. But that’s in c# ofc.

That’s not really designed for groups of enemies however, but it will give you a solid idea on how to alter the behaviour around your enemies distance and other such features.

Flying type enemies are included in tht code snippet too, but again it’s not really designed for a flock.

Best wishes,
Rob