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?
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.
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)
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.