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 sn24 · May 10, 2018 at 10:37 AM · rotationscripting problemclock

Clock script not working

Hello Everyone, I am trying to make a simple clock but the script is not working. But each time the game is played, all three hands turn to 180*. Any help?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Clock : MonoBehaviour {
 
     public float Hours, Minutes, Seconds;
     public GameObject hourHand, minuteHand, secondHand;
 
 
     void increment()
     {
         Seconds++;
     }
 
     private void Start()
     {
         InvokeRepeating("increment", 0f, 1.0f);
     }
     
 
     void Update()
     {
         if (Seconds == 60)
         {
             Seconds = 0;
             Minutes++;
         }
         if (Minutes == 60)
         {
             Minutes = 0;
             Hours++;
         }
         if (Hours == 13) Hours = 0;
         hourHand.transform.localRotation = new Quaternion(0f, 0f, (float)Hours * 6.0f, 0f);
         minuteHand.transform.localRotation = new Quaternion(0f, 0f, (float)Minutes * 6, 0f);
         secondHand.transform.localRotation = new Quaternion(0f, 0f, (float)Seconds * 6, 0f);
     }
 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Xarbrough · May 10, 2018 at 11:45 AM

Try this:

 using UnityEngine;
 
 public class Clock : MonoBehaviour
 {
     public GameObject hourHand, minuteHand, secondHand;
 
     public int hours, minutes, seconds;
 
     float timer;
 
     void Update()
     {
         if (timer >= 1f)
         {
             updateClock();
             updateVisuals();
             timer = 0f;
         }
         timer += Time.deltaTime;
     }
 
     void updateClock()
     {
         seconds++;
         if (seconds >= 60)
         {
             seconds = 0;
             minutes++;
         }
         if (minutes >= 60)
         {
             minutes = 0;
             hours++;
         }
         if (hours >= 13)
         {
             hours = 0;
         }
     }
 
     void updateVisuals()
     {
         hourHand.transform.localRotation = Quaternion.Euler(0f, 0f, -hours * (360f / 12f));
         minuteHand.transform.localRotation = Quaternion.Euler(0f, 0f, -minutes * (360f / 60f));
         secondHand.transform.localRotation = Quaternion.Euler(0f, 0f, -seconds * (360f / 60f));
     }
 }
 


I wouldn't use InvokeRepeating because of the string reference to a method, instead just track time with Time.deltaTime. Next, it is not the best idea to compare float values to discrete integers. In this case it was probably working because the values are set discretely in code, but in a lot of other cases you may run into floating point precision issues, where a comparison would fail.

Now the important aspect of updating the visuals: You were setting the values of a Quaternion directly, which is something you wouldn't normally do (maybe in very special cases and if you understand Quaternions well). In short, the z-value of the Quaternion does not correspond to the z-value, which you see in the Rotation field of the inspector. Unity shows Euler angles to the user, but expects a quaternion when setting the localRotation property. In this case, we simply want to do our math in euler degrees (360 degrees around the clock) and let Unity convert the value to a Quaternion via the Quaternion.Euler function.

Finally, the math for converting to "hour hand degrees" was not correct. We divide 360 degrees of a circle by 12 hour clock pieces and 60 minute/second pieces.

Here's a unity project showing the setup of the visual clock hands.

I hope this helps! Have fun!


clock.zip (3.2 kB)
Comment
Add comment · 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

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

173 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 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

if the fps is high the camera rotate much faster 1 Answer

Arm Rotation toward Cursor Script Help 1 Answer

After adding script for rotation to look up down, look left right stopped working,After writing code for the rotation for looking up down, looking right left stopped 0 Answers

How to make that the camera will not go through the walls? 1 Answer

CCTV behavior rotation issue 2 Answers


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