- Home /
Problem with quaternion rotation zxis
Hello, I have the following script attached to a cube which allows it to be moved upwards. It translates and rotates according to left and right keyboard input, the only problem is that it rotates in world space, not it's local space. How can I change this to make it rotate according to its own z-axis, not the worlds?
var momentum : float = 3;
//smooth : the lower the slower
var smooth;
function Update () {
var tiltAngle = -60.0;
var horiz = Input.GetAxis("Horizontal");
var target = Quaternion.Euler (0, 0, horiz*tiltAngle);
var zeroed = Quaternion.Euler (0, 0, 0);
//Increases forward momentum over time
momentum += Time.deltaTime*0.25;
//Left + right Translation and Upward Momentum
transform.Translate(horiz*0.25,Time.deltaTime*momentum, 0, Space.Self);
if (Input.GetButton("Horizontal")){
// Dampen towards the target rotation
smooth = 7.5;
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,Time.deltaTime * smooth);
}
if(Input.anyKey == false){
smooth = 2;
transform.localRotation = Quaternion.Slerp(transform.localRotation, zeroed,Time.deltaTime * smooth);
}
}
I'd appreciate any help you can give me with this, I'm really stumped by it.
Thanks
The reason it's not rotating along it's own axis is because localRoatation isn't the world space of this transform, it is the world space of it's parent's transform.
That's why It's not working, but I'm blanking on how to make it work as advertised (why this isn't an answer). I think I need more sleep.
Hrrm I see but this has no parent, it's just a cube object with the script on it. This is driving me craaaazy. Thanks for the quick reply though, go have a snooze!
With some help off 'deram_scholzara' on the 'Unity Community' the problem seems to be resolved by changing :
var target = Quaternion.Euler (0, 0, horiz*tiltAngle);
to:
var target = Quaternion.AngleAxis (horiz*tiltAngle, transform.forward);
Your answer
Follow this Question
Related Questions
Rotating on a plane 2 Answers
Rotating multiple points in space. 0 Answers
Apply a Rotation in World Space based on Quaternions 2 Answers
[Please!] Noob need's help with planet gravity movement. 2 Answers
how can i substract one axis by 180? 2 Answers