Changing Model's rotation to keep its left side toward the pivot as it goes in a circle

I wrote a little script for my scene that allows my ship to circle an island, and it seems to work alright.

Now I am trying to get the ship model to stay broadside to a pivot point in the center of the circle, so the cannons are ready to fire on the item. I have tried RotateAround, but the ship spins around very fast, and not relative to the broader circle it's making.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SailAroundIsland : MonoBehaviour {

private float currentTheta = 0; //start at 0 degrees
private int speed = 1;
private GameObject pivotPoint;

// Start is called before the first frame update
void Start()
{
pivotPoint = GameObject.Find("MainItem");
//StartCoroutine(MovePositionAlongCircle());
}

// should give the new vector
// now you just need to change theta accordingly
Vector3 getNextPointOnCircle(int radius, float theta, Vector3 currentPoint)
{
}

private void Update()
{
Vector3 nextPos = getNextPointOnCircle(radiusOfShipCircle, currentTheta, gameObject.transform.position);
nextPos.y = gameObject.transform.position.y;
gameObject.transform.position = nextPos;
currentTheta += Time.deltaTime;

//Finally if the angle is greater than 360 reset it to zero
//as we have gone all the way around the circle
if (currentTheta >= 360)
{
currentTheta = 0;
}
}
}


Check Quaternion.

Quaternions are used to represent rotations. It's usually the easiest way to do the rotation.

Try to use Quaternion.AngleAxis to Creates a rotation which rotates angle degrees around axis.

like:

gameObject.transform.rotation = Quaternion.AngleAxis(currentTheta, Vector3.up);


You can do this more simply, using the same calculation to give both the facing direction and the offset from the circle center.

private void Update()
{
// Compute our rotation first, yawing counter-clockwise.
Quaternion orientation = Quaternion.Euler(0, -angleDegrees, 0);

// Our offset from the center of the circle is then just
// some distance "rightward" according to that orientation.
Vector3 displacement = orientation * Vector3.right * radius;

// Apply these values to our transform.
transform.position = circleCenter + displacement;
transform.rotation = rotation;

// Increment our angle,

• Oops, meant to write orientation. Fixed above. Apr 10, 2022 at 21:58