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 lpincombe · Mar 07, 2019 at 10:57 AM · quaterniondirectional lightrotatetowards

Quaternion RotateTowards issues

Trying to rotate the directional light. I'm pretty sure I've set it up the same as the documentation at https://docs.unity3d.com/ScriptReference/Quaternion.RotateTowards.html but obviously I'm understanding something wrong. Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveSun : MonoBehaviour {
 
     public GameObject scripts; //drag the gameObject that contains the clock script into here
     public GameObject sun; //drag the directional light into here
 
     private int currentTime;
     private int sunriseTime = 28800;
     private int sunsetTime = 57600;
     private double sunSpeed = 1.0 / 240.0;
     
     private float noonAngle = 13f;
     private float noonTime;
     private Quaternion sunrise = Quaternion.Euler(0, 0, 0);
     private Quaternion noon;
     private Quaternion sunset = Quaternion.Euler(0, 180, 0);
     
     void Start () {
         currentTime = scripts.GetComponent<Clock>().secCount;
         noon = Quaternion.Euler(noonAngle, 90, 0);
         noonTime = (sunsetTime + sunriseTime) / 2;
 
         Debug.Log(noonTime);
     }
         
     void Update () {
 
         currentTime = scripts.GetComponent<Clock>().secCount;
         var step = sunSpeed * currentTime;
 
         if (currentTime >= sunriseTime && currentTime < noonTime)
         {
             sun.transform.rotation = Quaternion.RotateTowards(sunrise, noon, (float)step);
             Debug.Log("Sun is rising!");
         }
         else if (currentTime >= noonTime && currentTime < sunsetTime)
         {
             sun.transform.rotation = Quaternion.RotateTowards(noon, sunset, (float)step);
             Debug.Log("Sun is setting!");
         }       
         
     }
 }
 

The secCount is simply timeSpeed*Time.deltaTime in another script. The if statements are running but the sun goes to sunset at doesn't move (the game time starts at 12pm). Or if I start before 12pm the sun jumps to noon and doesn't move.

I think the issues are in my conditions somewhere. Just tried to use a while loop and broke Unity :(

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

Answer by xxmariofer · Mar 07, 2019 at 11:13 AM

test changing the quaternion lines to this

 sun.transform.rotation = Quaternion.RotateTowards( sun.transform.rotation, noon, (float)step);

unless i am missing something usnrise value is not being updated so every frame you are rotating from 0,0,0 to 0,0,180 so is always the same value.

Comment
Add comment · Show 6 · 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 lpincombe · Mar 07, 2019 at 11:21 AM 0
Share

Tried that and it still jumps :(

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class $$anonymous$$oveSun : $$anonymous$$onoBehaviour
 {
 
     public GameObject scripts; //drag the gameObject that contains the clock script into here
     public GameObject sun; //drag the directional light into here
 
     private int currentTime;
     private int sunriseTime = 28800;
     private int sunsetTime = 57600;
     private float sunSpeed = 1.0f / 240.0f;
     private float timeSpeed;
 
     private float noonAngle = 13f;
     private float noonTime;
     private Quaternion sunrise = Quaternion.Euler(0, 0, 0);
     private Quaternion noon;
     private Quaternion sunset = Quaternion.Euler(0, 180, 0);
 
     void Start()
     {
         currentTime = scripts.GetComponent<Clock>().secCount;
         timeSpeed = scripts.GetComponent<Clock>().timeSpeed;
         noon = Quaternion.Euler(noonAngle, 90, 0);
         noonTime = (sunsetTime + sunriseTime) / 2;        
     }
 
     void Update()
     {
 
         currentTime = scripts.GetComponent<Clock>().secCount;
         timeSpeed = scripts.GetComponent<Clock>().timeSpeed;
         sunSpeed = (1.0f / 240.0f) * timeSpeed;
 
         var step = sunSpeed * currentTime;
 
         if (currentTime >= sunriseTime && currentTime < noonTime)
         {
             sun.transform.rotation = Quaternion.RotateTowards(sun.transform.rotation, noon, step);
             Debug.Log("Sun is rising!");
         }
         else if (currentTime >= noonTime && currentTime < sunsetTime)
         {
             sun.transform.rotation = Quaternion.RotateTowards(sun.transform.rotation, sunset, step);
             Debug.Log("Sun is setting!");
         }
 
     }
 }
 
avatar image xxmariofer lpincombe · Mar 07, 2019 at 11:24 AM 0
Share

by jump you mean it reaches the noon/sunset rotation and doesnt move rotate again?

avatar image lpincombe xxmariofer · Mar 07, 2019 at 11:26 AM 0
Share

Yes, sorry if that wasn't clear. I hit the specified sunriseTime and the sun instantly moves to noon. Or if later, the clock hits the specified noon time and the sun instantly jumps to sunset position.

Show more comments

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

102 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

Related Questions

How to rotate towards the mouse on one axis while snapping 0 Answers

Object rotate to target in 2D? Not working properly. 0 Answers

Doing 2D only rotation using FromToRotation + RotateTowards 0 Answers

Quaternion.RotateTowards with 3 several rotation target 0 Answers

to rotate along z axis 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