Unity rotation issues - unity3d

I'm trying to have a model rotate 90 degrees when a button is pressed - should be simple, right?
Well, the entire system is a buggy mess for some odd reason. I would appreciate some help fixing it
transform.parent.rotation = Quaternion.Euler(transform.parent.rotation.x, transform.parent.rotation.y , transform.parent.rotation.z);
Instead, the model just rotates in random directions that seem like they shouldn't at all be related to my code.
I started up the game to rotate the model while it's in play-mode, but the way it rotates seems like it just suddenly changes out of the blue.
I'm really confused by this & would appreciate some help in fixing it

You code doesnt work like your think.
Quaterinion.Euler expects input in the form of Euler angles, but you are inputing the (x,y,z) of a Quaterinon which consists of (x,y,z,w) which is why you get really funky rotation.
https://docs.unity3d.com/ScriptReference/Quaternion.html
To get the current Euler Angles of your transform, simply use transform.eulerAngles (or in your case, transform.parent.eulerAngles)
var euler = transform.parent.eulerAngles;
transform.parent.rotation = Quaternion.Euler(euler.x, euler.y, euler.z);
However this doesnt change the rotation in anyway.
If you want to rotate 90 degrees around the Y-axis, you could add 90 like this
var euler = transform.parent.eulerAngles;
transform.parent.rotation = Quaternion.Euler(euler.x, euler.y+90, euler.z);
An even simpler way to rotate 90 degrees around Y is ofcourse
transform.Rotate(0, 90, 0);

Related

Find the offset between the rotation of a bone in Unity and in the original rig

Goal:
Calculate a rotation, that can be used, to correct the Transform of the bones, so they get rotated as expected when manually rotated.
In Detial:
Character's bone Transform is not imported to Unity correctily, ie. the left hand's Z axis does not looks towards the top of the hand, but backwards, while the right hand's Z axis looks forward.
The bones default rotation is not the same as you would expect if you have seen the rig in another application before.
I had the idea to create a class, that puts the character in T-Pose, then maps the bones rotation.
(I would assume that Unity does something similiar under the hood, that's how I got the idea.)
So the class would be used with an API like this:
/// This should return the rotation, where the bone pointing forward rotated by rotation.
/// Ie. when rotation = Quaternion.Identity the left hand should look forward.
Quaternion CorrectRotation(HumanBodyBones bone, Quaternion rotation)
{
return Quaternion.Inverse(tPoseRotations[bone]) * rotation;
}
The problem is, that I can't seem to find a good way to map theese rotations.
My last attempt was this:
Vector3 boneDirection = (boneTransform.position - parentTransform.position).normalized;
Quaternion mappedRotation = Quaternion.LookRotation(boneDirection, parentTransform.up);
As you can see in this image, with this method the hands look in the same direction, forward, but still not rotated correctly, they have an offset from the desired result. (The correct rotation is shown by the purple hands.)
When given other rotations, the hands follow with the same offset, so other then this unwanted offset they work correctly.
So basically, I would appriciate any help to fix my problem, either this way, or with a different solution, that works with different rigs.

I am trying to make my SCNNode rotate but I cannot seem to get it to work

My SCNNode is a compass that I want to rotate towards north.
compassNode = scene?.rootNode.childNodes[0]
I am successfully getting heading data and converting it to radians and storing it in a variable called angle.
angle = newHeading.trueHeading.toRadians
I have tried three methods now:
rotate = SCNAction.rotate(by: CGFloat(angle), around: SCNVector3(0, 1, 0), duration: 0.5)
compassNode?.runAction(rotate)
and
compassNode?.rotation = SCNVector4Make(0, 1, 0, Float(angle))
and
let oldTransform = compassNode?.transform
let newTransform = SCNMatrix4MakeRotation(Float(angle), 0, 1, 0)
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5
compassNode?.transform = SCNMatrix4Mult(newTransform, oldTransform!)
SCNTransaction.commit()
None of them makes any noticeable transformation to the compass. It appears to be still.
Running out of comment characters... Sry - have to put it in answer section.
Convert 90 degrees to radians, rotateTo that value. Rotating models can be confusing in 3D (for me anyway), sometimes it's easier to just use an SCNBox at first - it will have the correct rotation by default provided you're looking down -Z if you didn't change the camera view. You can turn on the the 3D outlines and sometimes that helps provide a better visual OR just texture map the box with different colors so that Red is the top, Green is front or whatever.
Then you can reshape the box, rotated it with different angles until you get the hang of rotateTo, rotateBy, rotateAround. I'm not a math guru, so unfortunately trial and error for me sometimes, but once I understand what's happening, then I can put the model in and know that my math works - then if I have to do some work on the model, such as pivot point or need to change the model - set Z-UP or whatever (this post: 59766878), then I'm focused on one thing at a time and not having a combination of problems to figure out.
I've also used lookAt and placed various (hidden) nodes at particular positions, so that I can see what X,Y,Z rotations look like at various points in a game and from different view points.

I can't get the right rotation of my transform in unity 3D

Hi everyone I have some little problem in Unity 3D. I am trying to do some walking simulation I have character and I have joints while she was walking I want to get the rotation of y. But when I try it I get some results like (transform of rotation in unity is -60 for example the value I get in program 0.059871 something like that how can I read the -60 the right value?)
Maybe you take the wrong properties, the Quaternion.y is a value from 0 to 1 (CMIIW)
but if you want to get you rotation in degree you can use Vector3.y by taking
var y = gameObject.transform.localEulerAngles.y;
Also, please attach your code before asking some code related issue

gameobject that rotate away from the facing direction of another game object

Trying to figure this out, but without success.
I have a AI that get to point X; facing that object, since I use transform.lookAt().
Now, I would like to turn the AI away, 180 degree, so it can face the same direction that the other agent is facing. I did try to add 180 to the transform once the AI get to destination but it doesn't work, sice the AI may arrive from any position, so the 180 degree rotation is not always the same as the direction in which the other agent is facing.
Is there a way to know or set, in which direction an object/AI/GameObject, is facing? Math wise, I believe it should be the vector3 related to when the object is imported in game, at 0.0.0 coordinates; although I can't really keep track of the orientation of every GO I have in the application; so I was hoping that there is some way to either set a direction, to which a GO is pointing at (even if it is not moving), or retrieve the orientation.
Have you tried looking at Transform.forward?
Edited for completeness:
Since transform.lookAt() is working for you currently, you can try this to face the target from any direction:
var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
and then add 180 degrees to the rotation

Get the position behind an object (locally) ? - Unity3D

I ran into a problem while making my complex(ish) camera behaviour (using Javascript). The problem that i have run to is that i am not able to get the position behind my player. Please don't tell me to use Vector3.back etc.. , because i want the position locally, to support turning. It won't work if the camera is always set to that position, cos i have a cool movement system in place.
I tried a number of approaches, and confused myself with the math. I also tried complex addition and subtraction. None of them worked at all.
I guess i am probably missing something quite simple, like a way to get into local coordinates etc.. ( or maybe a few math functions )
thanks in advance
-Etaash
You can get the forward vector of any transform, and if you negate that it is the backward vector. So on a script attached to the player you would have:
Vector3 backward = -transform.forward;
So to get a position, you could do something like this:
Vector3 pos = transform.position + (backward * dist); // where dist is a float to scale how far away from the position you want it