My Rotation completly stop itself after couple of seconds

I’m trying to make a good looking rotation, but here’s the problem, the rotation of my objects is stop after a couple of second and I don’t know why…

using UnityEngine;

public class Rotation : MonoBehaviour
{

    public Transform[] anchorPoints;

    private float rotationSpeed = 75f;
    private GameObject gameNode;
    private Quaternion RotationNode;
    private bool canRotate = false;

    private void Awake()
    {
        gameNode = GameObject.FindGameObjectWithTag("MovableGoal");
        RotationNode = transform.rotation;
    }

    private void Update()
    {

        if (gameNode != null)
        {
            foreach (Transform anchor in anchorPoints)
            {
                if (anchor.position.x <= gameNode.transform.position.x - 0.05)
                {
                    canRotate = true;

                }
                else if (anchor.position.y <= gameNode.transform.position.y - 0.05)
                {
                    canRotate = true;
                }
                else
                {
                    canRotate = false;
                }
            }
            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.forward, rotationSpeed * Time.deltaTime);
    }
}

6620689--754357--ccNUwqdlFa.gif

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

1 Like

Do what Kurt said, but on line 37 you specifically tell it to stop rotating if both of the previous conditions resolve to false. So that looks like an obvious place to start.

Thanks to both of you, my problem was on the line 37, I delted it and the problem was solved