- Home /
C# Unity 3D Lock Rotation but allow Rotation of Parent Object
I need to create a script that allows an Object to rotate while the other parented object Does not, how would i go about doing this?
Edit: Or would it be easier to create a script that mimics movement of an object?
Answer by syclamoth · Jan 24, 2012 at 10:28 AM
Well, if you don't want them to rotate together, are you sure that using transform parenting is the way to go? Copying an object's position is as simple as using
using UnityEngine;
public class FollowTransform : MonoBehaviour { // Assign in the editor the transform you want to follow; public Transform target;
void LateUpdate()
{
transform.position = target.position;
}
}
And that way it doesn't scale or rotate with the parent the way a transform child would.
Of course, there might be a very good reason for wanting them in the same hierarchy (for SendMessage purposes, etc.). In that case, the answer will change a little.
That piece of code gives me a compile error Edit: Placed it into a seperate file but it isn't showing a target variable in Unity
huh? I assumed that you knew how to declare public variables... Fine, I'll edit my answer to include all the fiddly details.
One EDIT: later: There, working yet?
That works perfectly, Thank you.
But im using a camera and its placed inside my object, would you know any idea about how i would make it move from a difference Pos? Adding an number to the X,Y,Z? and finally whats the difference between LateUpdate and Update
Edit: I created an empty GameObject and Parented that to the object, and got the camera to target the empty Gameobject and now got exactly what i wanted, Thank you so much for the help.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
What is wrong with this script? 2 Answers
Flip over an object (smooth transition) 3 Answers
Pushing More GUI Clips Than Popping 0 Answers
Killable NPC who can kill player 1 Answer