Search Unity

How to make an object move in a circular motion at constant speed?

Discussion in 'Scripting' started by Pennywise881, Apr 10, 2018.

  1. Pennywise881

    Pennywise881

    Joined:
    Nov 5, 2015
    Posts:
    26
    Hi there, I am trying to move an object in a circular motion using Sin and Cosine functions in unity. I am using the following code:

    Code (CSharp):
    1.     [SerializeField]
    2.     float moveSpeed, radius;
    3.     float angle;
    4.  
    5.     void Start()
    6.     {
    7.  
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         angle += moveSpeed * Time.deltaTime;
    13.         transform.position = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0) * radius;
    14.     }
    The problem that I am facing is that the bigger the value of the radius the faster the object moves. I assumed that the speed of movement will be determined by the moveSpeed variable in this case but that's not happening. So, any help is appreciated, thanks :)
     
    Boodums likes this.
  2. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    Update is a (almost) fixed amount of time... Give or take a tick or two

    if your radius is larger... there is more distance to travel in that Update.
    You're not really doing any [ speed = distance / time ] calculations here... you've just arbitrarily picked a number you'd like to use ( move speed).

    You need to divide by radius, not multiply.
     
  3. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Code (csharp):
    1. angle += (moveSpeed / (radius * Mathf.Tau)) * Time.deltaTime
    You are moving the angle at a constant rate, you need to divide by the distance it actually has to go, which is the circumference.
     
  4. Pennywise881

    Pennywise881

    Joined:
    Nov 5, 2015
    Posts:
    26
    Hi thanks for the reply. I did try this method out, and at one point it seemed like it resolved the problem but upon close inspection it causes the opposite result. For example when the radius is 1 it seems to be moving faster than when the radius is say 20. But I get why that happens, the speed is getting divided by 1. And what is 'Mathf.Tau'? Unity doesn't seem to recognize it.
     
  5. Pennywise881

    Pennywise881

    Joined:
    Nov 5, 2015
    Posts:
    26
    Do you mean I should divide the movespeed by the radius?
     
  6. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I imagine to your eye, it will appear as though the smaller radius is moving faster, but this is an optical illusion. It's accelerating faster, which tricks your brain.

    Sorry about the Mathf.Tau, apparently C# doesn't have it defined? No idea why, it's a ridiculously common mathematical constant.

    Code (csharp):
    1. angle += (moveSpeed / (radius * Mathf.PI * 2.0f)) * Time.deltaTime
     
    blueskied likes this.
  7. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    radius times tau.

    Personally, I would just divide like radius like you suggest, but the reason I used circumference instead is so that moveSpeed would be the speed at which the object actually moved, rather than being off by a facter of tau.
     
    FuguFirecracker likes this.
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You could consider just putting an empty gameobject at the center of your circle, make it the parent of the object you want to move, and just rotate that parent at a constant speed. You'd calculate the amount of rotation to achieve a given speed using math similar to what is already posted.
     
  9. clatat320

    clatat320

    Joined:
    May 10, 2022
    Posts:
    1
    For some reason, I never realized that I could just. Do that. Instead of searching for a method to make the object move and rotate on its own. Thanks for the tip, I needed it.
     
    Joe-Censored likes this.
  10. Boodums

    Boodums

    Joined:
    Jan 16, 2023
    Posts:
    6
    Thanks!