Circular Movement Using Forces Script

Hello, I have code a script to move an object in circles, without setting its transform.position component, in this script I only work with the rigidbody.velocity so that in the movement it can be affected by collisions. I’d like to share it with you so that i can learn from the feedback, or also if it helps you :).

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

public class CircularMoving : MonoBehaviour
{

    //Public variables
    public float speed = 10, initialDistance = 0;
    public bool moving = false;
    public Vector3 distance, perpendicularDistance, normalizedDistance;
    //Private Variables
    Transform center;
    Rigidbody playerRigidbody;

    // Start is called before the first frame update
    void Start()
    {
        center = GameObject.FindGameObjectWithTag("Center").transform;
        playerRigidbody = GetComponent<Rigidbody>();
        distance = center.position - transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            moving = !moving;
            initialDistance = distance.magnitude;
        }
        if(moving)
        {
            //We desactivate the gravity so it doesn't affect the movement
            playerRigidbody.useGravity = false;

            //Refresh the distance vector every second to get the proper normalized vector(normalizedDistance) below
            distance = center.position - transform.position;
            normalizedDistance = distance.normalized;

            //We get the perpendicular vector to normalizedDistance, this vector will be in every frame tangent to the circumference that the object
            //attached to this script will travel
            perpendicularDistance = new Vector3(-normalizedDistance.y, normalizedDistance.x, 0);

            //We modify the velocity component of our object matching it with the tangent vector, and multiplied by the distance to make the
            //circumference radio equal to the initial distance they were before starting the movement
            playerRigidbody.velocity = perpendicularDistance * initialDistance;
        }
    }
}

Hi and welcome.
Using rigidbody.MovePosition should enable interaction with colliders as well. If you dont want to do that, you’ll probably have to calculate the direction towards the next position on an imaginary circle and set the velocity to point in that direction, repeating this step every frame. That’s probably the closest approximation you’ll get, but it seems like a lot of effort.
If you just want something to orbit around something else plus collisions, then the easiest way to do that would be setting the orbiting object as child of the orbited object, and calling RotateAround in the Update loop.

It’s also generally not recommended to set rigidbody velocity at all. The velocity is a parameter that gets influenced by a lot of different forces (physics), so setting it basically prevents all that from happening, while still costing performance for calculating it. Rigidbodies are generally used to simulate physically accurate behavior, so if you dont want that you should not need them.

1 Like

Thanks a lot!! I didn’t even know about the rotateAround function, its works nicely on my project. My code upside gets obsolete with that function. At least i have learnt al lot about vectors and forces :wink:

1 Like