- Home /
Rotating objects in local space?
I'm trying to make this script that rotates and object back and forth but the object that is being rotated is a child of the object the script is on. While the script does rotate the child object back and forth, its facing is always wrong. I've tried a lot of different combinations of transforming the target rotation from one coord system to another at various stages of the calculation, but it never ends up correct. My script is below, can anyone see what's wrong with it?
public float openSpeed;
public float shakeSpeed; //how fast to shake
public float openDist;
public int shakeCount; //number of times to shake
public float shakeRot; //how many degrees the object will shake
Transform lid;
int curShakeCount;
int shakeDir;
Vector3 startRot;
Vector3 tarRot;
// Use this for initialization
void Start () {
shakeDir = 1;
curShakeCount = 0;
Renderer renderer = gameObject.GetComponent<Renderer>();
lid = gameObject.transform.GetChild(0);
startRot = lid.TransformVector(lid.eulerAngles); //transform rotation from local to world space
tarRot = Quaternion.AngleAxis(shakeRot, Vector3.forward) * startRot;
tarRot = lid.InverseTransformVector(tarRot); //transform tar rotation from world to local space
}
// Update is called once per frame
void Update () {
if(curShakeCount <= shakeCount)
{
lid.rotation = Quaternion.LookRotation(Vector3.RotateTowards(lid.eulerAngles, tarRot, Time.deltaTime * shakeSpeed, 0));
if(Mathf.Abs(Vector3.Distance(lid.eulerAngles, tarRot)) < .1f)
{
shakeRot = -shakeRot;
tarRot = Quaternion.AngleAxis(shakeRot, Vector3.forward) * startRot;
tarRot = lid.InverseTransformVector(tarRot);
}
}
}
I ended up going with a different approach to the solution by not making the object I'm rotating a child. Would be nice to know how to do it with a child object, but the below works in the mean time.
public float openSpeed;
public float shakeSpeed; //how fast to shake
public float openDist;
public int shakeCount; //number of times to shake
public float shakeRot; //how many degrees the object will shake
public float destThreshold;
Transform lid;
int curShakeCount;
int shakeDir;
Vector3 startRot;
Quaternion tarRot;
// Use this for initialization
void Start () {
shakeDir = 1;
curShakeCount = 0;
Renderer renderer = gameObject.GetComponent<Renderer>();
lid = gameObject.transform.GetChild(0);
lid.parent = null;
startRot = lid.eulerAngles;
lid.Rotate(Vector3.up, shakeRot);
tarRot = lid.rotation;
lid.rotation = Quaternion.Euler(startRot);
}
// Update is called once per frame
void Update () {
if(curShakeCount <= shakeCount)
{
//lid.rotation = Quaternion.Euler(Vector3.Lerp(lid.eulerAngles, tarRot, Time.deltaTime * shakeSpeed));
lid.rotation = Quaternion.RotateTowards(lid.rotation, tarRot, Time.deltaTime * shakeSpeed);
if($$anonymous$$athf.Abs(Quaternion.Angle(lid.rotation, tarRot)) < destThreshold)
{
Quaternion temp = lid.rotation;
shakeRot = -shakeRot;
lid.Rotate(Vector3.up, 2*shakeRot);
tarRot = lid.rotation;
lid.rotation = temp;
}
}
}
Answer by tanoshimi · Sep 20, 2015 at 07:59 PM
Sounds like on line 29 you want to be setting the transform's localRotation, not its rotation...
http://docs.unity3d.com/ScriptReference/Transform-localRotation.html
Your answer
Follow this Question
Related Questions
scripting error 1 Answer
C# Children of GameObject has it's script disabled after SetActive(false) and SetActive(true) 1 Answer
How can I create a random keycode generation ? 0 Answers
How to create a score manager script involving awarding points from multiple objects? 2 Answers
Script that works, throws in errors 2 Answers