- Home /
Transform rotation
Hi, I have a problem with the transform.rotation. I want one object to repeat an other object's rotations, but not completely, I need the rotation on the y Axis to be modified. This is what I have right now
rotx = GameObject.FindWithTag("knee").transform.rotation.x;
roty = GameObject.FindWithTag("knee").transform.rotation.y;
rotz = GameObject.FindWithTag("knee").transform.rotation.z;
//print(rotx + " " + roty + " " + rotz);
this.transform.rotation = GameObject.FindWithTag("knee").transform.rotation;
So, this code works great right now, I need to modify it in some way as to apply each axis rotation individually, what I would like it to look like is something like this
this.transform.rotation.x = rotx;
this.transform.rotation.y = roty;
this.transform.rotation.z = rotz;
Is this possible to achieve in any way?
Thanks ;)
Ok, I tried a bunch of other things, nothing made me happy. I almost found a solution to satisfy my needs though:
Vector3 pivotPoint = new Vector3(0,0,0);
pivotPoint = GameObject.FindWithTag("follower").transform.position;
this.transform.RotateAround(pivotPoint, Vector3.right, y);
So this rotates the object just as I need it to rotate. But I need another type of function, I don't want to input the Amount for the object to rotate, I need my object to rotate until a certain value, could someone help me here? I'm kind of running out of ideas :/
Answer by SinisterSound · Oct 28, 2011 at 10:42 PM
Yes, but you will have to store them in a temporary variable or create one on the fly when setting your new rotation.
Instead of:
this.transform.rotation = GameObject.FindWithTag("knee").transform.rotation;
You should do:
this.transform.rotation = new Quaternion(rotx, roty, rotz, rotw);
From there you can modify any of the passed in parameters as you please :)
Answer by Rod-Green · Oct 28, 2011 at 11:25 PM
Another way is to store the current Rotation value and modify as needed and then finally reapply at the end of the method i.e.
Quaternion curRotation = this.transform.rotation;
// Order is important!! A rotation that rotates euler.z degrees around the z axis, euler.x degrees around the x axis, and euler.y degrees around the y axis (in that order).
curRotation.eulearAngles.z += rotz;
curRotation.eulearAngles.x += rotx;
curRotation.eulearAngles.y += roty;
this.transform.rotation = curRotation;
In your case though it can be simplified to basically:
using UnityEngine;
using System.Collections;
public class RotateScript : MonoBehaviour
{
public Transform otherTrans;
void Start()
{
if(otherTrans == null)
{
enabled = false;
return;
}
}
void Update()
{
float rotx = otherTrans.rotation.eulerAngles.x;
float roty = otherTrans.rotation.eulerAngles.y;
float rotz = otherTrans.rotation.eulerAngles.z;
//print(rotx + " " + roty + " " + rotz);
// Tweak roty in some way -- i.e.
roty = 0.0f;
//Then apply the new values
this.transform.rotation = Quaternion.Euler(rotx, roty, rotz);
}
}
Your solution skews the model in a very strange way :/
There's no skewing in my code (though I failed to include .eulerAngles prop) however are you doing other transforms also? i.e. non uniform scaling? I think you should add a longer example of the code you're having trouble with. This might help people try and provide a solution.
edited to give a more complete example
After searching for some more solutions I now think I approached the question from a wrong angle. What I have is a human model (with parts as leg, hand, etc. attached) with an Animation attached to it (he bends his knee). I can go to the Animation window and look at the leg animation and modify the Rotate.y in there and everything works fine. Do you know how can I modify that value from a script?
So if you just want to modify the Y rotation of an object via a script? Are you talking about editing animation data? or muting animation data and applying procedural data to an animation channel?
Your answer
Follow this Question
Related Questions
Rotation/position question. 1 Answer
Min-Angle Constraint Script Causes Jitter 0 Answers
A node in a childnode? 1 Answer