- Home /
How can I rotate a bone from script? Applications: Move crane arm, point gun...
When a key is pressed, I want my bone to rotate. Here is my code so far:
using UnityEngine;
using System.Collections;
public class myAction : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.A)){
GetComponent<Animation>().Play("animation1");
}
}
void LateUpdate(){
if (Input.GetKeyDown (KeyCode.B)) {
(GetComponent<Transform>().Find("RootBone/Bone3")).eulerAngles = new Vector3 (0, 0, 90);
}
}
}
After Edit, I get the following error:
NullReferenceException: Object reference not set to an instance of an object rotateBones.LateUpdate () (at Assets/rotateBones.cs:20)
Have you made sure that the bone Transform is found successfully? Use a Debug.Log line to check if all parts of your code are called. Also, maybe it won't work because you check for the same key press in both Update and LateUpdate.
You are rotating your script instanced transform, I think. You need to access the actual transform with GetComponent.
Debug.Log(bone.Rotation);
bone.eulerAngles = new Vector3 (0, 0, 90);
Debug.Log(bone.Rotation);
I edited the code as you suggested, but now I am getting another error saying that the Object reference not set to an instance of an object rotateBones.LateUpdate ().
Answer by Cherno · Jul 02, 2015 at 09:18 PM
Try it like this:
using UnityEngine;
using System.Collections;
public class myAction : MonoBehaviour {
public Transform bone;
// Use this for initialization
void Start () {
bone = GetComponent<Transform>().Find("RootBone/Bone3");
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.A)){
GetComponent<Animation>().Play("animation1");
}
}
void LateUpdate(){
if (Input.GetKeyDown (KeyCode.B)) {
if(bone != null) {
bone.eulerAngles = new Vector3 (0, 0, 90);
}
else {
Debug.Log("bone is null!");
}
}
}
}
Answer by dhanyfling · Jan 28, 2020 at 05:22 PM
I use script to animate Bone, I use character created with MAKEHUMAN software, i mean to rotate a bone to 90 degrees, the bone rotated well but not animated, the bone rotated instantly so we can't see the rotating progress, i want this bone rotate process finish after 3 second, how to make that happen, this is my current code using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BoneAccess : MonoBehaviour
{
public Transform bone;
// Start is called before the first frame update
void Start()
{
bone = GetComponent<Transform>().Find("MakeHuman default skeleton/root/spine05/spine04");
}
// Update is called once per frame
void Update()
{
if(bone != null) {
bone.localEulerAngles = new Vector3 (130, 0, 0);
}
else {
Debug.Log("bone is null!");
}
}
}
Try setting bone.localEulerAngles to a Vector3.Lerp:
void Update()
{
float speed = 1.0f;
if (bone != null)
{
bone.localEulerAngles = Vector3.Lerp(bone.localEulerAngles, new Vector3(130, 0, 0), Time.deltaTime * speed);
} else
{
Debug.Log("'bone' is null!");
}
}
This speed may not work for you, but you can change that value to something that seems right for your case.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200928im_/https://answers.unity.com/themes/thub/images/avi.jpg)