Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fockah · Apr 29, 2019 at 05:34 PM · movement2d sprites

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;

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fockah · Apr 29, 2019 at 05:40 PM 0
Share

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

Sorry.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 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;
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fockah · Apr 29, 2019 at 06:18 PM 0
Share

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.

avatar image Hellium Fockah · Apr 29, 2019 at 07:12 PM 0
Share

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.

avatar image Fockah · Apr 29, 2019 at 08:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Want sprite to move to mouse click position instead of teleporting or moving a few pixels 1 Answer

Bullet follows player mouvement when It instintiates. 1 Answer

Character facing the position of mouse cursor (2d platformer),Sprite changing based on mouse position 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges