Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 WizzleDonker · Jan 26, 2013 at 03:42 AM · rotatebeginnersimplesun

Problem with Simple Script - Rotation and Data Types

Basically, this was supposed to be a small script for the sun of one of my projects. It simply sets the location of the sun in the sky with the location it is supposed to represent, and then proceeds to cycle the sun along the X axis at the rate determined by the 'hoursInDay' variable'

fortunately, the first part of the functionality works perfectly, though the second part has a ridiculous problem which I feel so stupid for not being able to solve. the 'rot' variable will not go to anything other than zero. I can manually enter the rotation speed in the Rotate function, but cannot use the variable no matter what I try... casting the original variable, ect. Would anyone be kind enough to nudge a newbie in the right direction?

 #pragma strict
 
 var hoursInDay = 1;
 
 private var rot = 15 / (hoursInDay * 60 * 60 / 24);
 
 function Start () {
 
     //Setting the sun to the right position
     var time = (System.DateTime.Now.Hour - 6);
     
     transform.rotation.eulerAngles = new Vector3(15 * time, 0, 0);
 }
 
 function Update () {
     transform.Rotate(rot * Time.deltaTime, 0, 0);
 }
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
0
Best Answer

Answer by Matthew0123 · Jan 26, 2013 at 04:25 AM

 #pragma strict
  
 var hoursInDay = 1;
  
 private var rot = 15 / (hoursInDay * 60 * 60 / 24);
  
 function Start () {
  
     //Setting the sun to the right position
     var time = (System.DateTime.Now.Hour - 6);
  
     transform.rotation.eulerAngles = new Vector3(15 * time, 0, 0);
 }
  
 function Update () {
     transform.Rotate(rot * Time.deltaTime, 0, 0);
 }

There is an a very common error here. I will state one of the most common rookie error is you CANNOT assign a VAR a value OUTSIDE of a function. So your vars are not set to a value and they're undefined. Do this to correct it.

 #pragma strict
  
 var hoursInDay: float;
  
 private var rot: float;
  
 function Start () {
     //Assigning Variables
     hoursInDay = 1;
     rot = 15 / (hoursInDay * 60 * 60 / 24);
  
     //Setting the sun to the right position
     var time = (System.DateTime.Now.Hour - 6);
  
     transform.rotation.eulerAngles = new Vector3(15 * time, 0, 0);
 }
  
 function Update () {
     transform.Rotate(rot * Time.deltaTime, 0, 0);
 }

This should fix it. If there is another error comment and I will look at it again.

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 Matthew0123 · Jan 26, 2013 at 04:27 AM 0
Share

I just notice another error. Because you need to keep updating the position you need to put "rot = 15 / (hoursInDay 60 60 / 24);" inside the Update function so it will change every frame. If you put it in the Start function it will only be called one time when the script is started.

avatar image WizzleDonker · Jan 26, 2013 at 06:06 AM 0
Share

Thank you very much! This solved the problem.

avatar image Matthew0123 · Jan 26, 2013 at 06:07 AM 0
Share

Welcome. I was shock no one gave you an answer sooner due to how common of a mistake this is.

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

10 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

Related Questions

Why does my player accelerate and not stay constant 1 Answer

Top Down 3D moves in unexpected ways. 1 Answer

Problem with collecting plants 0 Answers

newbie 2d simple sprite rotation 1 Answer

make my object face the direction of movement? 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