- Home /
What is the best way to rotate a ship slowly towards a target with torque using physics?
I have a floating boat that I am trying to control with pure physics, however thus far it hasn't been working that well, beyond the buoyancy which works perfectly.
At the moment, I've got a vague target look-at that rotates, however it tends to over-shoot and then won't stop when it gets to the target, resulting in a weird orbit. What I want to do is keep it from over-shooting when it's lined up with the target, and then move to the destination and finally stop, until the destination changes.
This is the code producing the not-quite-working effect:
TargetLocation = TempTarget.position;
Vector3 Heading = TargetLocation - transform.position;
float direction = Vector3.Dot(Heading, -transform.forward);
float Distance = Vector3.Distance(TargetLocation, transform.position);
Debug.Log("Direction Status: " + Mathf.Sign(direction));
if (Distance > 1.5f)
{
// Is the Target in front or behind me? //
// In Front //
if (Mathf.Sign(direction) == 1)
{
currentThrustPower = 600;
} else
{
currentThrustPower = 0;
}
if (direction < 0.55f)
{
torquePower = 1400;
}
else
{
torquePower = 0;
}
} else {
currentThrustPower = 0;
torquePower = 0;
}
RTorque = new Vector3(0, 0, torquePower);
Vector3 forceToAdd = -transform.up * currentThrustPower;
boatRB.AddRelativeTorque(RTorque);
boatRB.AddForceAtPosition(forceToAdd, waterJetTransform.position);
Answer by Brijs · Jun 10, 2016 at 01:07 PM
Calculate the angle between velocity of boat and target direction of boat(target.transform.position - boat.transform.position)
var angle = Vector3.Angle(rigidbody.velocity , target.transform.position - boat.transform.position);
When boat orbits around target this angle must be around 90.
So when this angle gets nearer to 90 decrease the speed of boat.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
FIXED: How can I stop a collider from tunneling? 2 Answers
C# Water Rotation 0 Answers
Punching objects (C#) 1 Answer