Moving to certain direction
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 :D
Your question is hard to understand. Are you rotating object or how is it following guide? I mean, it gets strange to think about object always moving forward, clamped to 45 degree translation in two axis based on the location of guide if object doesn't rotate. And if object rotates, wouldn't it make sense to limit the rotation speed of object to avoid hard turns ins$$anonymous$$d of trying to modify forward direction?
Please explain with what you have (code or project or images help) and what you want to achieve is without getting technical (for example "guide moves around, object is trying to follow guide but should not make hard turns").
If you want to move "forward and right" (45 degrees right of forward) you could do something like:
Vector3 fwdAndRight = Vector3.Normalize(Vector3.forward + Vector3.right);
Vector3 direction = transform.TransformVector(fwdAndRight);
transform.position += direction;
Thanks for quick reply and sorry for my bad explanation :D
Lets say the object is a boat and its always rotating towards the guide. Guide moves around, boat is following guide and stops when it reaches the guide. The problem is if the boat is near the guide and the guide changes radically the movement direction (to behind the boat for example) the boats's rotation cannot keep up with the guide and the boat starts to move sideways. I'm trying to limit the moving angle so that this wouldn't happen. This is hard to explain but hopefully this code helps a bit:
void $$anonymous$$ove(float angleToGuide, float limitedAngle, float limitedAngleNegative) {
// object slows down when it gets closer to guide
if (angleToGuide <= 30 && angleToGuide >= -30 && distanceToGuide > 0 && distanceToGuide <= 4) {
maxSpeed = 1f;
moving = true;
} else if (angleToGuide <= 30 && angleToGuide >= -30 && distanceToGuide > 4) {
maxSpeed = 3f;
moving = true;
}
if (distanceToGuide == 0) {
torque = 0f;
moving = false;
}
//accelerating and deccelerating
if (torque < maxSpeed && moving) {
torque += 0.5f * Time.deltaTime;
} else if (torque > maxSpeed && moving) {
torque -= 1f * Time.deltaTime;
}
//here is where I try to limit the turning angle
//if (moving) {
if (moving && angleToGuide > -30 && angleToGuide < 30) {
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, target.position, moveStep);
} else if (angleToGuide <= -30 && moving) {
// move object to -limited direction
} else if (angleToGuide >= 30 && moving) {
// move object to limited direction
}
}
}
Answer by Statement · Oct 24, 2015 at 03:22 PM
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);
return adjustedGuide;
}
And then where you had your movement code, it could look something like this:
if (moving)
{
// Move toward adjustedGuide
transform.position = Vector3.MoveTowards (transform.position,
GetAdjustedGuide(angleToGuide), moveStep);
}