Im trying to rotate a transform on his axis via C#. But when executed this really not move… Can someone explain me why? How quaternions works?
Im using this code (later ill add a non frame rate dependent mode.)
public GameObject SunComponent;
void Start()
{
LightComponenet = SunComponent.GetComponent<Light>();
}
void Update()
{
SunRotation = SunComponent.transform.rotation.x;
SunRotation = SunRotation++;
}
I was expecting the objec rotating really fast, instead it simply not moves… Why?
Because “SunRotation” is NOT the object’s transform. After you increase SunRotation, you need to apply it to the gameObject:
SunComponent.transform.rotation.x = SunRotation;
I’m assuming SunRotation is defined as an int somewhere.
SunRotation = SunComponent.trasnform.rotation.x
The above assigns the value of x to SunRotation as it is not a reference, but a value type. Any changes to SunRotation won’t be applied back to the value in SunComponent as it has copied the value to your new variable SunRotation.
To re-apply the value you need to manually set the value in SunComponent:
SunComponent.transform.rotation.x = SunRotation;
or even faster just :
SunComponent.transform.rotation.x++;
As a quick note on formatting, variables should be camelCase and methods/classes should be CamelCase
This Code will just make the sun rotate about itself however. If you’re trying to get it to rotate about a point its slightly more complicated. To get the sun to move in an arc over the centre of the terrain i implemented the following and attached the script to my sun object:
public class SunMove : MonoBehaviour
{
GameObject terrain;
public float speed;
float terrainLength;
float terrainWidth;
float angle;
float radius;
float cos;
float sin;
Vector3 centre;
float sinRad;
float cosRad;
// Use this for initialization
void Start()
{
terrain = GameObject.FindGameObjectWithTag("Terrain");
if (speed == null)
{
speed = 0.01f;
}
terrainLength = terrain.GetComponent<Terrain>().terrainData.size.z;
terrainWidth = terrain.GetComponent<Terrain>().terrainData.size.x;
transform.position = new Vector3(terrainWidth * 0.5f, 0, 0);
angle = 0f;
centre = new Vector3(terrainWidth * 0.5f, 0, terrainLength * 0.5f);
radius = Vector3.Distance(centre, transform.position);
}
// Update is called once per frame
void Update()
{
if (speed != 0)
{
angle += speed;
cos = Mathf.Cos(angle);
sin = Mathf.Sin(angle);
sinRad = sin * radius;
cosRad = cos * radius;
gameObject.transform.position = new Vector3(centre.x, sinRad + centre.y, cosRad + centre.z);
}
else
{
transform.position = new Vector3(centre.x, 125, -125);
}
gameObject.transform.LookAt(centre);
}
}
Solved this problem by myself (after two hours of study about quaternoins and euler angles.)
The solution is really simple: converting quaternions to vector3 angles (eulers) and applying the result. In code is really simple:
public class SunTime : MonoBehaviour {
public Vector3 SunRotation = new Vector3 (1, 0, 0);
public GameObject SunComponent;
//Each update i add 1 euler rotation on the X axis.
void Update()
{
SunComponent.transform.Rotate(SunRotation);
}
}