Hi. Kinda easy question but how can I make an object move to a certain direction relative to object’s angle? Basically I just want to replace “transform.forward” to “transform.forward - 45 degrees”.
I have an object that is following another object (lets call it guide) and I would like to limit the object’s maximum turning angle to 45 degrees, so when the angle between the object and the guide exceeds 45 degrees, the object cannot turn any tighter (moves to direction forward - 45 degrees instead of following the guide).
I’ve got everything else figured out except for that, even though that should be the easiest part
Create a function that calculates where the guide should be. Note: I don’t know which coordinate system your game uses. This assumes that the boat is moving along the Y plane (Y is up, X and Z are directions the boat moves in).
Vector3 GetAdjustedGuide(float angleToGuide)
// The black arrow in comment graphic
Vector3 offset = target.position - transform.position;
// Clamp the angle between -30 and 30 degrees
float clampedAngle = Mathf.Clamp(-30, 30, angleToGuide);
// Create an euler rotation on Y axis to be within -30 to 30 degrees
// Depending on your game setup, you may need to choose a different axis, like the Z axis.
Quaternion clampedRotation = Quaternion.Euler(0, clampedAngle, 0);
// Rotate a forward direction with the same length as offset
// Depending on your game setup, you may need to choose a different forward, like up.
Vector3 clampedDirection = clampedRotation * new Vector3(0, 0, offset.magnitude);
// Transform the direction to take into account the boats current rotation and positon
Vector3 adjustedGuide = transform.TransformPoint(clampedDirection);
And then where you had your movement code, it could look something like this:
// Move toward adjustedGuide
transform.position = Vector3.MoveTowards (transform.position,