- Home /
Rotation around an axis, how unity handle this?
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?
Answer by AironeneroTechnologies · Nov 23, 2014 at 06:17 PM
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);
}
}
Answer by FairGamesProductions · Nov 23, 2014 at 10:44 AM
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;
Visual studio says: impossible to modify a value that doesnt is a variable.
(and i already know that.)
I think i should use quaternions (as writed in the question).
Answer by Kannonfodder · Nov 23, 2014 at 12:14 PM
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);
}
}
Technically correct, but please note two things:
1: The OP uses C#. In C#, you cannot directly assign a value to transform.rotation.x, because rotation is not a public variable, but a property. You'll get the CS1612 compiler error stating "Cannot modify return value of... etc.".
2: Rotations in Unity are represented as quaternions, which are complex numbered matrices that can uniquely represent all rotations without gimble lock, but which do not make intuitive sense. Accessing and working with its individual values as though they were Euler angles does not make sense.
I should have actually looked at the variable (property) in question before answering, didn't realise it was accessor only...oops
This is why I hate C#. It's unnecessarily complicated... JS is so much simpler.
In UnityScript (and not JS) you not have interfaces and other advanced thing that are really useful when making advanced relations with different classes.
Anyway the code not works, im pretty sure i need to use quaternion, but how?
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
transform.Rotate will not work 1 Answer
Question on Forcing a Specific Rotation 1 Answer
How to rotate a GameObject around another object according to the mouse cursor position in C# 0 Answers
How do I rotate an object to the cursor?Where is the mistake in the code? 1 Answer