protected override Vector3 Calculate(SteeringAgent _agent) { Vector3 force = _agent.CurrentForce; Vector2 offset = new Vector2(Random.Range(-jitter, jitter), Random.Range(-jitter, jitter)); force += _agent.Right * offset.x; force += _agent.Up * offset.y; return(force); }
public override Vector3 Calculate(SteeringAgent _agent) { Vector3 force = _agent.CurrentForce; behaviours.ForEach(weighted => { force += weighted.behaviours.Calculate(_agent) * weighted.weighting; }); return(force); }
public void UpdateAgent(SteeringAgent _agent) { Vector3 force = Calculate(_agent).normalized; _agent.UpdateCurrentForce(force); Quaternion rotation = Quaternion.Slerp(_agent.Rotation, Quaternion.LookRotation(_agent.CurrentForce != Vector3.zero ? _agent.CurrentForce : Vector3.one), Time.deltaTime); Vector3 movement = (_agent.Forward + force * _agent.Speed) * Time.deltaTime; Vector3 position = Vector3.SmoothDamp(_agent.Position, movement + _agent.Position, ref _agent.velocity, _agent.MovementSmoothing); _agent.SetPosandRot(position, rotation); }
public override Vector3 Calculate(SteeringAgent _agent) { Vector3 force = _agent.CurrentForce; Vector2 offset = new Vector2(Random.Range(-jitter, jitter), Random.Range(-jitter, jitter)); force += _agent.Right * offset.x; force += _agent.Up * offset.y; // May the force be with you return(force); }
public override Vector3 Calculate(SteeringAgent _agent) { Vector3 force = _agent.CurrentForce; foreach (Vector3 direction in SteeringAgentHelper.DirectionsInCone(_agent)) { if (Physics.Raycast(_agent.Position, direction, out RaycastHit hit, viewDistance)) { // Visualise the collision Debug.DrawLine(_agent.Position, hit.point, Color.red); // Interpolate the normal by the forward over the normalRatio variable force += Vector3.Lerp(_agent.Forward, hit.normal, normalRatio); } } // f**k the force return(force); }
/// <summary> /// Runs the calculations for the position and rotations of the passed /// agents using the force calculated in the <see cref="Calculate(SteeringAgent)"/> function /// </summary> /// <param name="_agent"></param> public void UpdateAgent(SteeringAgent _agent) { Vector3 force = Calculate(_agent).normalized; _agent.UpdateCurrentForce(force); // Calculate the rotation using Slerp, the current roation and the force for the target Quaternion rotation = Quaternion.Slerp( _agent.Rotation, Quaternion.LookRotation(_agent.CurrentForce != Vector3.zero ? _agent.CurrentForce : _agent.Forward), Time.deltaTime * 10f); // Calculate the position by finding the correcting movement then damping the difference. Vector3 movement = (_agent.Forward + force * _agent.Speed) * Time.deltaTime; Vector3 position = Vector3.SmoothDamp( _agent.Position, movement + _agent.Position, ref _agent.velocity, _agent.MovementSmoothing); // Apply the calculated rotation and position _agent.ApplyPosandRot(position, rotation); }
// Default parameters are paramters that don't need to speicifally be passed in, // if they aren't, the set value will be used, otherwise the one passed in will be. // Default parameters also MUST be at the end of the parameter list. public static Vector3[] DirectionsInCone(SteeringAgent _agent, bool _forceRecalculate = false) { //Determined if this function has't been run before if (coneDirections == null || _forceRecalculate) { List <Vector3> newDirections = new List <Vector3>(); // Loop through every direction that has already been caluclated in the sphere foreach (Vector3 direction in directions) { // Calculate the angle between the forward of the agent // and this direction... if it is less than the view angle, we can add // it to the list if (Vector3.Angle(direction, _agent.Forward) < _agent.ViewAngle) { newDirections.Add(direction); } } coneDirections = newDirections.ToArray(); } return(coneDirections); }
protected abstract Vector3 Calculate(SteeringAgent _agent);
/// <summary> /// The function that the behaviours need to override to calculate /// their forces for the agent. /// </summary> /// <param name="_agent"></param> /// <returns></returns> public abstract Vector3 Calculate(SteeringAgent _agent);