Why doesn`t it run circles?

Hello.

I don’t understand why the following code doesn’t let the object circle.
The object “dot” is just a Sprite in an two-dimensional environment.

It should be moving around the center, right?

Thus from north over east to south and over west to north.

Many thanks for your help in advance.

    Vector3 origin = new Vector3(1,1,0);
    float r = 1.0f;
    float angle= 0.0f;

void Update()
{

  //float x = r * Mathf.Cos(Mathf.Deg2Rad * angle);
  //float y = r * Mathf.Sign(Mathf.Deg2Rad * angle);

    float x = r * Mathf.Cos(angle);  
    float y = r * Mathf.Sign(angle); 
   
    Vector3 v = new Vector3(x, y, 0);
    v += origin;
    angle += 1.0f;
    dot.transform.position = v;

}

I believe the script works fine, you are just increasing angle too much and keep in mind that Cos and Sinuse radians, so if you increase angle by 1 each frame, only 6/7 frames will be needed to make a complete circle (which represents about 0.1 second if your game runs at 60 FPS)

Important note, you have used Sign instead of Sin, is it a typo mistake?

Here is my suggestion:

public Vector3 Origin = new Vector3(1,1,0);
public float Radius = 1.0f; // Radius of the circle in world units
public float RotationSpeed = 1 ; // 1° per second

private float angle= 0.0f;

void Update()
{    
     float x = Radius * Mathf.Cos(angle * Mathf.Deg2Rad);
     float y = Radius * Mathf.Sin(angle * Mathf.Deg2Rad);
     Vector3 position = Origin + new Vector3(x, y, 0);
     dot.transform.position = position;
     angle += RotationSpeed * Time.deltaTime;
}

dot.transform.position = f;
should be: dot.transform.position = v;

Sorry.

Thank you very much.

Now its working as it is supposed to do.

Yes, “Sign” is a typo, im sorry. Two typos in 10 lines of code…

But in my Code i used Sin, it happened while i retyped it in here.

I set
float rotationSpeed = 1;
and
angle += rotationSpeed;

and it works. rotationSpeed * Time.deltaTime was a little bit to slow.

To be honest, i don´t get it why it didn´t work before.
I used Sin(angle * Mathf.Deg2Rad) at first place and switched after that to Sin(angle).

Does Unity sometimes ignore changes in scripts?

A few weeks ago I had an issue in which i changed inside my script a public value from 10 to 20 but the Unity-Editor always showed me and used 10.
Even when i started my program with CTRL+P it used 10 instead of 20.

Debug.Log(value) gave me the old 10 instead of the new 20.

It was hard to find that error.

I have screenshots to prove it.

After deleting all .meta-files it worked normal.

Thank you again for the quick help.

I will test RotationSpeed * Time.deltaTime tomorrow.

I’ve spent quite some time with Unity. Only small 2D projects, in which I invested a good amount of time.

I implemented A*Star, adapted for Unity, for example.

I coded “the usual suspects” and that.

I’m not a Unity crack, by no means, but I’m no longer a greenhorn in 2D programming.
I do hope it at least…

But I didn’t notice this behaviour so far. I believe you, absolutely.

Unity won’t change the value in the inspector…<< “In the Inspector.” Accepted.

But: also when running the script? It still used 10 instead of 20.

My programm crashed because of that, it tried to access values >10 in an array.

I couldn’t believe it myself and discussed the case with my brother, a professional QA man in Unity-made games.

Too bad, I should have sent in the project for a review.

I hope this isn’t expected behaviour, I would use a different word for that…

Oh, and a few days later I could not create scripts anymore in the Unity-Editor.

I clicked right and chose “new Script” but Unity didn’t recognice it as a C#-file at all.

No chance, i wrote xyzscript.cs, but nada. "

After a few tries I reinstalled Unity, I didn’t know what to do else.