//-------------------------------------------------------------------------------------------------------------- private void FixedUpdate() { // Find out the relation of the current movement direction to the decided direction up = transform.up; angleDiff = Vector3.Angle(up, Context.DecidedDirection); cross = Vector3.Cross(up, Context.DecidedDirection); // Do not let the cross.z be close to zero, otherwise, the character stuck when the decision direction is // anti-parallel if (!Mathf2.Approximately(Context.DecidedDirection.sqrMagnitude, 0f) && Mathf2.Approximately(cross.z, 0f)) { cross.z = Mathf.Sign(Random.Range(-1f, 1f)); } // Orient towards decision direction using torque Body2D.AddTorque(cross.z * Torque * angleDiff); // Translate along oriented direction using the force (which may be with you, of course) if (ObjectiveAsSpeed >= 0 && ObjectiveAsSpeed < Context.DecidedValues.Count) { velocity = Context.DecidedValues[ObjectiveAsSpeed] * Speed; velocity = velocity > Speed ? Speed : velocity; } else { velocity = Speed; } Body2D.AddForce(velocity * up); }
//-------------------------------------------------------------------------------------------------------------- private void Update() { if (Mathf2.Approximately(Context.DecidedDirection.sqrMagnitude, 0)) { return; } // Orient towards decision direction transform.rotation = Quaternion.LookRotation(Context.DecidedDirection, Up); // Translate along oriented direction if (ObjectiveAsSpeed >= 0) { velocity = Context.DecidedValues[ObjectiveAsSpeed] * Speed; velocity = velocity > Speed ? Speed : velocity; } else { velocity = Speed; } transform.position += Time.deltaTime * velocity * Context.DecidedDirection; }