- Home /
Why can't I assign a rotation to a rigidbody Object?
I have a character completely made out of parts, with rigid bodies and joints. I am trying to assign the rotation of the LowerTorso to the rotation of the Camera. It is not working. What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LowerTorsoRotation : MonoBehaviour {
public GameObject Camera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.rotation = Quaternion.Euler(transform.rotation.x,Camera.transform.rotation.y,transform.rotation.z);
}
}
Can you confirm you have attached this component to the LowerTorso game object? Also, are their any joints that connect the LowerTorso to another rigidbody? If so, do the connected rigid bodies (or any connected to them) have some kind of limitation that would counteract this change?
I would also suggest a sanity check e.g.: Debug.Log("Changing rotation to:" + Camera.transform.rotation.y);
to make sure your code is actually running.
I have verified that it is attached and is running, and yes, there are joints connected to other rigid bodies, but they are not limited. I also realized that the rotation resets, even though for X and Z they are not supposed to change.
Have you tried using Rigidbody.MoveRotation?
https://docs.unity3d.com/ScriptReference/Rigidbody.MoveRotation.html
Answer by Pupaak · Oct 28, 2021 at 01:12 PM
You have to set the rotation with Rigidbody.rotation.
Something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LowerTorsoRotation : MonoBehaviour {
public GameObject Camera;
private Rigidbody rigidbody;
// Use this for initialization
void Start () {
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
rigidbody.rotation = Quaternion.Euler(transform.rotation.x,Camera.transform.rotation.y,transform.rotation.z);
}
}