I’m searching a way to make my object rotate around another one, but the movement isn’t the best to look
I’ve dropped my code and a gif of the result that I have currently
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour
{
public GameObject[] gameNodes;
public Transform anchorGameNode;
public Transform anchor;
public float rotationSpeed;
private bool canRotate = false;
public Quaternion RotationNode;
private void Awake()
{
RotationNode = transform.rotation;
}
public void Update()
{
if (anchorGameNode != null)
{
foreach (GameObject gameNode in gameNodes)
{
if ((anchor.position.x == anchorGameNode.position.x))
{
canRotate = true;
}
if (canRotate)
{
RotateGameNodes(gameNode);
}
}
}
else
{
transform.rotation = RotationNode;
}
}
private void RotateGameNodes(GameObject gameNode)
{
transform.Rotate(transform.rotation.x, transform.rotation.y, -rotationSpeed * Time.deltaTime);
gameNode.transform.RotateAround(transform.position, -transform.position, rotationSpeed * Time.deltaTime);
var dir = -(transform.position - gameNode.transform.position);
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
gameNode.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}