- Home /
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;
}
dot.transform.position = f; should be: dot.transform.position = v;
Sorry.
Answer by Hellium · Apr 29, 2019 at 05:43 PM
I believe the script works fine, you are just increasing angle
too much and keep in mind that Cos
and Sin
use 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;
}
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 $$anonymous$$athf.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 ins$$anonymous$$d of 20.
Debug.Log(value) gave me the old 10 ins$$anonymous$$d 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 believe having angle += RotationSpeed * Time.deltaTime;
is important. It makes the rotation independent from framerate, meaning more time would be needed to make a complete rotation if you have a slow computer otherwise.
If you think the rotation is slow, simply increase the RotationSpeed
in the inspector.
Does Unity sometimes ignore changes in scripts?
It should not.
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.
This is the expected behaviour. Supposing you have declared your variable as follow:
public int value = 10 ;
Once Unity interprets your script and serialize the new component (meaning it "saves" the value into persistent state), even if you change the value the value assigned when declaring the value (`public int value = 20;`), Unity won't change the value in the inspector unless you click on the little gear at the top-right corner of the component and hit Reset
.
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 program$$anonymous$$g.
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 ins$$anonymous$$d of 20.
$$anonymous$$y 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.
Your answer
