0
\$\begingroup\$

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 int radiusOfShipCircle = 100;
    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)
    {
        float rad = theta * Mathf.Deg2Rad;
        return new Vector3(radius * Mathf.Cos(rad), 0, radius * Mathf.Sin(rad));
    }

    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;
        }
    }
} 
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

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);
\$\endgroup\$
0
\$\begingroup\$

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,
    angleDegrees += (speed / radius) * Time.deltaTime * Mathf.Rad2Deg;    
    if (angleDegrees >= 360)
    {
        angleDegrees -= 360.0f; // Subtract, don't set to zero.
    }
} 
\$\endgroup\$
3
  • \$\begingroup\$ Hey its DMGregory ! le rep master of stack overflow :), I was looking at one of your posts about projectile trajectories to try and polt crabs a few days ago. When assigning the rotation transform.rotation = rotation; where is the var rotation coming from ? I tried moding this script like this but the ship stays in place. \$\endgroup\$
    – Doug Ray
    Apr 10, 2022 at 21:35
  • \$\begingroup\$ Oops, meant to write orientation. Fixed above. \$\endgroup\$
    – DMGregory
    Apr 10, 2022 at 21:58
  • \$\begingroup\$ hmmm I tried this solution but it didn't seem to work for me. I must have made some mistake but was able to get it another way \$\endgroup\$
    – Doug Ray
    Apr 14, 2022 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .