- Home /
How to freeze child gameobject rotation???
Hello I have 2 gameobject the first one is parent and the second is child ,, if the parent gameobject rotate the child gameobject rotate too, and I want to freeze the child object from rotation...
thx..
Answer by aldonaletto · Oct 13, 2011 at 03:03 PM
I didn't test this, but I believe that you can just set the child transform.rotation to a fixed value in LateUpdate to eliminate any parent-originated rotation. You can save the initial child rotation at Start, like below (attach this to the child):
var iniRot: Quaternion;
function Start(){
iniRot = transform.rotation;
}
function LateUpdate(){
transform.rotation = iniRot;
}
NOTE: If the parent is being moved by physics you may have problems, because physics runs at its own pace, and a cycle may occur between LateUpdate and the scene rendering.
Liking the simple solution. But what if I want to freeze the X and Z axis only? I get an error when using transform.rotation.y = rotation.y ?
Transform.rotation is a Quaternion, not those nice angles we see in the Inspector's Rotation field (they are actually the Transform.eulerAngles), and modifying its components only produces weird and unexpected results.
You could try to save the Transform.eulerAngles ins$$anonymous$$d of Transform.rotation, and only restore x and z:
var iniRot: Vector3;
function Start(){
iniRot = transform.eulerAngles;
}
function LateUpdate(){
iniRot.y = transform.eulerAngles.y; // keep current rotation about Y
transform.EulerAngles = iniRot; // restore original rotation with new Y
}
But this may fail miserably if the object rotates "too much" - as you may have already noticed, the Inspector's Rotation field may jump weirdly when any of the angles crosses multiples of 90 degrees.
Answer by firasdeep · Oct 14, 2011 at 03:39 AM
this script work . attach with child object ... :)
function Update () {
var rotation = Quaternion.LookRotation(Vector3.up , Vector3.forward);
transform.rotation = rotation;
}
You need to modify the rotation of the attached rigidbody as opposed to transform. IE:
RigidBody rb;
function void Awake() {
rb = GetComponent<RigidBody>();
}
function void FixedUpdate() {
rb.rotation = rotation;
}
:0)