- Home /
Setting a specific rotation to an object C#
Hi there guys, I have been struggling a lot with both vector3 Rotations and Quaternions, what i am trying to achieve is simply a 'Set Rotation' I dont want to add nor subtract to or from the current rotation, i have tried this with both Vector3s transform.Rotate, and Quaternions Euler, and Quaternions rotation, but have had no success.. i simply want an object to be set to a rotation once this method is called. how can that be achieved easily? This is my method i am trying to call, and the the 'can' objects rotation to 0, 85, -25
public void pickupCan()
{
can.transform.parent = holder.transform;
can.transform.localPosition = Zero;
pickedCan = true;
can.GetComponent<Rigidbody>().useGravity = false;
can.GetComponent<Rigidbody>().isKinematic = true;
//Here is the issue
can.transform.Rotate(new Vector3(0, 85, -25));
}
You can just set the rotation itself. The Quaternion object has a function to convert Euler angles into Quaternions.
can.transform.rotation = Quaternion.Euler(0, 30, 0);
Answer by NoBrainer-David · May 16, 2017 at 02:19 PM
transform.Rotate will add a rotation. To set the rotation, use
transform.rotation = Quaternion.Euler(0, 85, -25);
Thank you this has helped somewhat, but now i at least understand what is going wrong, i would like the object I'm trying to rotate, to face a certain direction after being parented to my character, but what i can see is happening is, this rotation is setting by world rotation, not its parents rotation, how can i over come this? i have made a video demonstration of what happens to better instruct of what i am trying to achieve.
In the video i have demonstrated that the object does not rotate to the parents rotation in the hierarchy rather it rotates still to the world's rotation.
At the start of the video i am showing pressing 'F' to call this piece of code transform.rotation = Quaternion.Euler(0, 85, -25);
Video Link HERE https://youtu.be/$$anonymous$$PjqB5RVTsA
In other words i want 'F' and on pickup to have the nozzle of the water can to face outward from the player with the rotation of 0 x 85 x -25
You can chain the rotations with the * operator. If you have a reference to your player, try:
can.transform.rotation = player.transform.rotation * Quaternion.Euler(0,85,-25);
Your answer
![](https://koobas.hobune.stream/wayback/20220612122125im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate a gameobject using 2 fingers (Twist) 0 Answers
how to reorient the axes ? 1 Answer
How to rotate object from center of the object? 1 Answer
Rotating a character's velocity? 3 Answers