How do i rotate a model a predefined degrees - roblox

I want to know how I can rotate a model 90 degrees without going over. I am working on making an animation for a model and I need to know.

Your question is a little hard to understand but if you are asking how to rotate a model 90 degrees from a script here is how.
local model = workspace.Model --Get your model
--Make sure you set the PrimaryPart of the model (what you want the model to rotate around)
local PrimaryPartCFrame = model:GetPrimaryPartCFrame() --Get the CFrame of the primary part
local rotation = CFrame.Angles(math.rad(90),0,0) --Create a CFrame rotated 90 degrees on the x axis
local RotatedCFrame = PrimaryPartCFrame * rotation --Creates new rotated cframe that is the primary part rotated
--(see https://developer.roblox.com/en-us/articles/CFrame-Math-Operations to understand)
model:SetPrimaryPartCFrame(RotatedCFrame) --Rotate the entire model with the new cframe

Related

apply a rotation relative to any reference space - Unity

is there a way to apply a rotation for any arbitrarily orientation (Object A's rotation relative to Object B's local orientation)
I know I can simply get desired result if i do setParent thing, but i want to get without it
In my situation, my character holds water bottle and moves around the room holding it so I set its rotation like this
transform.rotation = mainCamera.transform.rotation;
but As bottle top pointing to the sky seems more natural, I want it to rotate 90 degree by its own X-axis
I tried below codes but it dosent work as i intended
`originalRotation = transform.rotation;
Quaternion rotationDelta = Quaternion.FromToRotation(Vector3.forward, mainCamera.transform.forward);
transform.rotation = rotationDelta * originalRotation;`
Yes, Like i said above if i make my character parent to the bottle will solve the issue, but i want to
do it without it.
Thank you
Try this one:
transform.forward = mainCamera.transform.up;

Unity rotation issues

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);

Unity3D manipulate object orientation with respect to original orientation

I encountered another issue with hand-object interaction using a Leap Motion device. In particular, I use the LM to perform orientation manipulations on virtual objects.
I want to use the difference between the current and last orientation of the hand to manipulate the object:
Quaternion relativeOrientationHands = Quaternion.Inverse(currentHandOrientation) * updatedHand;
transform.rotation = transform.rotation * relativeOrientationHands;
It works fine, however the problem is that, let's say I rotate the object 180 degrees around the x axis (after that, y is pointing downwards). If I release the object afterwards, and grasp it again, the orientation changes get from then on applied to the new orientation of the object which is super confusing to use. (Original: turn hand to the left - object turns counter-clockwise; After: turn hand to the left - object turns clockwise)
Unfortunately, I don't know the math to fix it, but maybe someone can help me out. How can I apply the orientation changes of the hand to my virtual object, with respect to the default object orientation using Quaternions. Maybe, something with normal vectors?
EDIT:
The 3 figures illustrate my problem. The first figure shows the object in its original orientation (x-right, y-upwards). I turn the cube 180 degrees by rotating my hand 180 degrees clockwise around z (blue axis), after I release it, x is pointing to the left and y is pointing downwards (figure 2). If I grasp the object (figure 3) in its current orientation, and let's say I would want to perform a 90 degrees clockwise rotation around the y-axis. In figure one, I could do that by moving my hand 90 degrees clockwise. However, I changed the orientation of the cube with my previous manipulation. Therefore, I would have to perform a 90 degrees counter-clockwise rotation with my hand to move the object in clockwise direction, because the y-axis is flipped (figure 2-3). I always want that turning your hand 90 degrees clockwise results in a 90 degrees clockwise rotation of the object regardless of its current orientation.
Thank you very much
Best
R.Devel

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

UIImageView rotation affecting position?

I have a UIImageView that is set to move up and down the screen with the value of the accelerometer, using the following code:
ship.center = CGPointMake(ship.center.x, ship.center.y+shipPosition.y);
Where shipPosition is a CGPoint set in the accelerometerDidAccelerate method using:
shipPosition.y = acceleration.x*60;
Obviously this works fine, it is very simple. I run into trouble when I try to something equally simple, vary the rotation of the image depending on its acceleration. I do this using:
ship.transform = CGAffineTransformMakeRotation(shipPosition.y);
For some reason this causes a very strange thing to happen, in that the image snaps back to its origin every time the main method is called. I can see frames where the image moves to where it should be, but then instantly snaps back.
This problem only happens when I have the rotation line in, commented out it works fine. I have no idea what is going on here, I have done this many times for different apps and i never had such a problem. In fact I copied my code from a different app I created where it works fine.
EDIT:
What really confuses me is when I change the angle of the rotation from the acceleration to the position of the ship using:
ship.transform = CGAffineTransformMakeRotation(ship.center.y/10);
When I do this, the ship actually rotates based on the accelerometer but does not move, which is crazy because a changing ship.center.y means the position of the ship is changing, but it's not!!
You should set the transform of you view back to CGAffineTransformIdentity before you set his center coordinates or frame and after that apply the new transformation.
The frame property returns the transformed coordinates of a view if it is transformed and not the true (well actually the transformed are true) coordinates.
Quote from the docs:
Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
Update/Actual Answer:
Well the actual problem is
shipPosition.y = acceleration.x*60;
Since you set the y pos in accelerometerDidAccelerate.
The acceleration won't remember it's old value. So if you move your device it will get a peak and as you slow down it will decelerate again.
Your ship will be +/-60 at the highest acceleration speed but will be 0 when you stop moving your device and shipPosition.y will be 0.
CGAffineTransformMakeRotation expects angle in radians, not in degrees.
1 radian = M_PI / 180.0 degrees