public PairWeightSteering() { weight = 0; steering = new Steering(); }
public override Steering GetSteering() { // Create the structure to hold our output Steering steering = new Steering(); if (target == null) { enabled = false; return(steering); } // Get the direction to the target Vector3 direction = target.transform.position - transform.position; float distance = direction.magnitude; //Check if we are there, return no steering if (distance < targetRadius) { onTarget = true; return(steering); } if (distance > targetRadius + 2.4f) { onTarget = false; } if (!onTarget) { float targetSpeed; //If we're outside de slowRadius, then go max speed if (distance > slowRadius) { targetSpeed = agent.maxSpeed; } //Otherwise we calculate a scaled speed else { targetSpeed = agent.maxSpeed * distance / slowRadius; } Vector3 targetVelocity = direction; //desired velocity combines speed and direction targetVelocity = direction; targetVelocity.Normalize(); targetVelocity *= targetSpeed; //Acceleration tries to get target velocity steering.linear = targetVelocity - agent.velocity; steering.linear /= timeToTarget; //Check if the acceleration is too fast if (steering.linear.magnitude > agent.maxAccel) { steering.linear.Normalize(); steering.linear *= agent.maxAccel; } } return(steering); }
// Use this for initialization public override Steering GetSteering() { // Create the structure to hold our output Steering steering = new Steering(); if (target == null) { enabled = false; return(steering); } // Get the naive direction to the target float rotation = target.transform.eulerAngles.y - agent.orientation; //float rotation = target.transform.eulerAngles.y - transform.eulerAngles.y; rotation += opposite ? 180 : 0; // Map the result to the (-pi, pi) interval rotation %= 360.0f; if (rotation < -180 || rotation > 180) { rotation += rotation < -180 ? 360 : -360; } float rotationSize = Mathf.Abs(rotation); //Check if we are there, return no steering if (rotationSize < targetRadius) { return(steering); } float desiredRotation; // If we are outside the slowRadius, then use // maximun rotation if (rotationSize > slowRadius) { desiredRotation = maxRotation; } // Otherwise calculate a scaled rotation else { desiredRotation = maxRotation * rotationSize / slowRadius; } // The final target rotation cambines speed // (already in the variable) and direction desiredRotation *= rotation / rotationSize; // Acceleration tries to get to the target rotation steering.angular = desiredRotation - transform.eulerAngles.y; steering.angular /= timeToTarget; // Check if the acceleration is too great angularAccel = Mathf.Abs(steering.angular); if (angularAccel > agent.maxAngularAccel) { steering.angular /= angularAccel; steering.angular *= agent.maxAngularAccel; } // Output the steering // steering.linear = Vector3.zero; return(steering); }