- Home /
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 :(
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.
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!");
}
}
}
by jump you mean it reaches the noon/sunset rotation and doesnt move rotate again?
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.
Your answer
