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);

I see. Yeah there is no drag applied to my player, so the player is always faster in the air, because friction comes into play on the ground. You slowed him in the air to fix it, I sped him up on the ground by removing friction while in motion. I will fool around with your solution and see if that works better for me, but right now my movement works great.

For sure, they're both viable in isolation, but who knows if removing friction will result in some unintended consequence later in development - which is why I'm so wary about workarounds like this. I feel your solution is a bit more sound.

1 Answer

1

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.

I think we're doing the same but opposite. Leads to the same result. With dev there are many paths to the same solution, neither necessarily better than the other, just whatever works for you.