public Vector3 SteeringArrive(GameObject target, float timeStep) { float dist = (target.transform.position - character.transform.position).magnitude; //in the inner radius, stop if (dist < ArrivalRadius) { _characterVelocity = Vector3.zero; hasArrived = true; } else if (dist < SlowDownRadius) { Vector3 desiredVel = target.transform.position - character.transform.position; float distance = desiredVel.magnitude; float mag = AdditionalVector3Tools.map(distance, 0, 50, 0, MaxSpeed); //CharacterVelocity = mag*desiredVel*timeStep; _characterVelocity = Vector3.Lerp(_characterVelocity, Vector3.zero, timeStep); hasArrived = true; } else if ((target.transform.position - character.transform.position).magnitude > SlowDownRadius) { hasArrived = false; } return(_characterVelocity); }
public void Align2(float targetOrientation, float timeStep) { float rotation = targetOrientation - character.transform.rotation.eulerAngles.y; rotation = AdditionalVector3Tools.mapAngleToRange(rotation); Vector3 charAngles = character.transform.rotation.eulerAngles; float rotationSize = Mathf.Abs(Mathf.Abs(targetOrientation) - Mathf.Abs(transform.rotation.eulerAngles.y)); //if we are within the range of satisfaction, stop rotating and return the targets rotation if (rotation < slowDownOrientation) { charAngles.y = targetOrientation; character.transform.rotation = Quaternion.Euler(charAngles); return; } float sign = Mathf.Sign(rotation); float goalVelocity = (sign * maxAngularVelocity) * (rotation * sign) / (sign * slowDownOrientation); float angularAcceleration = (goalVelocity - characterAngularVelocity) / timeToTarget; float angle = charAngles.y; if (Mathf.Abs(angularAcceleration) < Mathf.Abs(maxAngularAcceleration)) { characterAngularVelocity = characterAngularVelocity + angularAcceleration * timeStep; } else { characterAngularVelocity = (sign) * angularAcceleration; } if (characterAngularVelocity < maxAngularVelocity) { charAngles.y = angle + characterAngularVelocity * timeStep; character.transform.rotation = Quaternion.Euler(charAngles); return; } }
void Align(float targetOrientation, float timeStep) { float rotation = targetOrientation - character.transform.rotation.eulerAngles.y; rotation = AdditionalVector3Tools.mapAngleToRange(rotation); Vector3 charRotation = character.transform.rotation.eulerAngles; //if we are within the range of satisfaction, stop rotating and return the targets rotation if (Mathf.Abs(Mathf.Abs(targetOrientation) - Mathf.Abs(transform.rotation.eulerAngles.y)) < satisfactionRotation) { charRotation.y = targetOrientation; character.transform.rotation = Quaternion.Euler(charRotation); return; } //rotation sign float sign = Mathf.Sign(rotation); //now we compute the goal angular velocity float goalVelocity = (sign * maxAngularVelocity) * rotation / (sign * slowDownOrientation); //current character velocity float currCharVelocity = Vector3.Magnitude(character.rigidbody.velocity); //compute the angular acceleration float angularAcceleration = (goalVelocity - currCharVelocity) / timeToTarget; if (Mathf.Abs(angularAcceleration) > Mathf.Abs(maxAngularAcceleration)) { angularAcceleration = maxAngularAcceleration * sign; } //ensure angular velocity sign is the same as rotation if (Mathf.Sign(maxAngularVelocity) != sign) { angularVelocity *= sign; } angularVelocity += angularAcceleration * timeStep; if (Mathf.Abs(angularVelocity) > maxAngularVelocity) { angularVelocity = Mathf.Abs(maxAngularVelocity) * sign; } charRotation.y = character.transform.rotation.eulerAngles.y + angularVelocity * timeStep; character.transform.rotation = Quaternion.Euler(charRotation); }
public Vector3 SteeringFlee(Vector3 target, float timeStep) { // FixRotation(); characterAcceleration = (character.transform.position - target); characterAcceleration.y = 0; characterAcceleration = Vector3.Normalize(characterAcceleration) * maxAcceleration; _characterVelocity = _characterVelocity + (characterAcceleration * timeStep); _characterVelocity = AdditionalVector3Tools.Limit(_characterVelocity, MaxSpeed); return(_characterVelocity); }
public void SteeringFlee() { characterAcceleration = (character.transform.position - _targetGameObject.transform.position); characterAcceleration.y = 0; characterAcceleration = Vector3.Normalize(characterAcceleration) * maxAcceleration; _characterVelocity = _characterVelocity + (characterAcceleration * Time.fixedDeltaTime); _characterVelocity = AdditionalVector3Tools.Limit(_characterVelocity, MaxSpeed); flattenYtoZero(); rigidbody.velocity = _characterVelocity; }
public void SteeringFlee(Transform target) { //get the direction of the target and normalize it characterAcceleration = (character.transform.position - target.transform.position).normalized; characterAcceleration *= maxAcceleration; _characterVelocity = _characterVelocity + (characterAcceleration * Time.fixedDeltaTime); _characterVelocity = AdditionalVector3Tools.Limit(_characterVelocity, MaxSpeed); //FixRotation(); flattenYtoZero(); character.rigidbody.velocity = _characterVelocity; }
public Vector3 SteeringSeek(Vector3 targetPos, float timeStep) { if (!hasArrived) { // FixRotation(); characterAcceleration = (targetPos - character.transform.position); characterAcceleration.y = 0; characterAcceleration = Vector3.Normalize(characterAcceleration) * maxAcceleration; _characterVelocity = _characterVelocity + (characterAcceleration * timeStep); //current velocity + desiredAcceleration* time _characterVelocity = AdditionalVector3Tools.Limit(_characterVelocity, MaxSpeed); } return(_characterVelocity); }