I have inherited some code that simply does this....
transform.rotation = m.rotation;
I know that my model will always be standing upright and therefore it only needs to rotate around on one axis. How can I only change the Y rotation.
I can't seem to get this to work because I get stuck right away when I try:
transform.rotation = Quaternion.Euler(m.rotation.x, m.rotation.y, m.rotation.z);
Which does not align it the same as:
transform.rotation = m.rotation;
I was hoping to then just change the m.rotation.y
. Any reason this doesn't work?
The m
value looks like this
public MarkerInfo(string name, bool isVisible, Vector3 position, Quaternion rotation, Vector3 scale){
this.name = name;
this.isVisible = isVisible;
this.position = position;
this.rotation = rotation;
this.scale = scale;
}
CodePudding user response:
If m
stands for a c# class and not a uniy one, changing its rotations will have no effect. You need to apply your rotation to a unity Transform
, that normally is an instance of a component of a gameobject of the scene. That way you can see in the scene the changes you apply to the transform of that specific gameobject.
I think you need to know what unity gameobject does MarkerInfo
manipulate or stands for, get its Transform
with gameObject.transform
and rotate that.
With transform.RotateAround or manipulating directly yout transform's Transform.eulerAngles you should be able to get your rotation.
Take into account that this line: transform.rotation = Quaternion.Euler(m.rotation.x, m.rotation.y, m.rotation.z);
you are trying wont do what I think you think it does. The x,y,z of a quaternion are not the euler angles.
To apply that rotation to the transform you need to do transform.rotation = m.rotation
, so the fact those line do not do same make perfect sense.
CodePudding user response:
The best ways to do this are by using Quaternion.Euler, which converts a Vector3 to a Quaternion (Unity's confusing variable for storing rotation), or by using Quaternion.Set, which sets the values of a Quaternion similar to Vector3.Set. The first one has 2 declarations: 1 where you pass a vector3 variable, and another where you pass 3 ints/floats. The following 2 lines of code both do the same thing using both declarations.
transform.rotation = Quaternion.Euler(Vector3.up);
transform.rotation = Quaternion.Euler(0, 1, 0);
The second method works like this (I don't know what the fourth one does, but it's typically 1):
// transform.Set(x, y, z, 1);
transform.Set(0, 45, 0, 1);
transform.Set(transform.x 1, transform.y, transform.z, 1);
Quaternions are confusing and annoying at first, but Unity comes with a lot of fancy rotation-focused functions that coultn't be possible without them. You can use Quaternion.Euler to convert a Vector3 to a Quaternion and then use those functions on them. They could come in handy later. You can find them here under "public/static methods".
CodePudding user response:
I suppose you want player to have same forward vector as other object but keep the original up vector?
I would avoid Euler Angles as much as possible because they are "glitchy" and try like that:
var forward = m.forward;
forward.y = 0;
transform.rotation = Quaternion.LookRotation(forward, Vector3.up);
just resolve the case
m.forwad == Vector3.down || m.forward == Vector3.up
and it should be working