How to rotate a child object independently from it's parent so that it maintains it's transform integrity and shape
I have a rotatable parent object, that rotates using transform.Rotate(), and also a to be rotatable child object.
what I want to achieve is for the child object to rotate with the rotation of it's parent, which is fine, but also rotate on it's own, which results in some weird deformity to the shape of the child. as in the picture
code parent
public class Platform : MonoBehaviour {
public float rotation;
public float force;
private Quaternion origin;
private Rigidbody rigidBody;
// Use this for initialization
void Start () {
origin = transform.rotation;
rigidBody = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
float forward = Input.GetAxis ("Vertical");
float side = Input.GetAxis ("Horizontal");
transform.Rotate (new Vector3 (forward * rotation,0,-side * rotation));
transform.rotation = Quaternion.Slerp (transform.rotation, origin, .1f);
if (Input.GetKey (KeyCode.O)) {
print ("force");
rigidBody.AddForce (transform.up*force, ForceMode.Force);
}
}
}
code child
public class player : MonoBehaviour {
public Transform sun;
public float rotation;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float forward = Input.GetAxis ("Vertical");
float side = Input.GetAxis ("Horizontal");
transform.Rotate (new Vector3 (forward * rotation,0,-side * rotation));
}
}
This is all detailed in the manual, under the Transform section.
Your answer
Follow this Question
Related Questions
Unity 5 - How to addtorque to rigidbody without overriding axis constraints? 0 Answers
Changing parent at runtime rotates object. 0 Answers
2D Rigidboy, have child keep upright but allow parent to rotate 1 Answer
How could I make the children of an object face the direction it is moving on the y-axis? 0 Answers
How to rotate an object in certain angle with respect to its parent 0 Answers