Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JJDeveloper · Jun 13, 2015 at 01:36 PM · c#rotationrotatebone

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)

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jun 13, 2015 at 05:04 PM 0
Share

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.

avatar image meat5000 ♦ · Jul 01, 2015 at 10:38 PM 0
Share

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);
avatar image JJDeveloper · Jul 02, 2015 at 05:21 PM 0
Share

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 ().

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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!");
                             }
                 
             }
 
         }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!");
                              }
         }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_2000legobrick · Feb 25, 2020 at 08:41 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Rotate a child bone during animation? 1 Answer

Trouble with Vector3.Angle() and RotateAround() 1 Answer

Rotation using exponential functions 1 Answer

Why the player is not rotating and moving at the same time ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges