Search Unity

Problem with script to access bone

Discussion in 'Scripting' started by MechEngi, Jun 7, 2018.

  1. MechEngi

    MechEngi

    Joined:
    Jun 4, 2018
    Posts:
    4
    console.png log.png Hello everyone,
    i just started using unity3D and i'm currenly working on a script to access a bone and change the rotation of the leg from the up-arrow on the keyboard.
    I wrote a script that makes sense for me but doesn't work.

    The error are compiled on the log.

    There are some fundamentals concepts i don't understand with unity:
    - are you supposed to use the name of the object as it is in the hierarchy or in the asset when writing the script?
    - also i'm not really sure if you have to create an instance of the game object or if it is pre existing and you just have to call it.
    - I didn't get the difference between the transform and the gameObject classes, even if it appears that the transform is most preferably used for the rotation operations.
    If anybody has the answers that would be great.
    Thanks all

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4.  
    5. public class CharacterController : MonoBehaviour
    6. {
    7.     //first test for the upper leg: the objective is to control the rotation of the upper leg with a keyboard value for the rotation
    8.     // around the x-axis.
    9.     public float rotateVel = 100;
    10.     public Transform leg;
    11.     public GameObject body;
    12.     float fowardInput, turnInput, rotationX;
    13.  
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.     void ModifyAngle()
    19.     {
    20.         //Obtain the leg
    21.         leg = body.transform.Find("Upper_Leg_L");
    22.         //Obtain the input rotation:
    23.         rotationX = Input.GetAxis("Vertical");
    24.         rotationX *= Time.deltaTime;
    25.         leg.transform.Rotate(0, rotationX, 0);
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         ModifyAngle();
    31.     }
    32. }
    33.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    - The name as it is in the hierarchy
    - You need a reference to the gameobject. If it doesn't exist you use instantiate to create an instance (and get the reference to it via the returned value from the function), if it does exist you need some way of referencing it (inspector value, parameter passed by physics engine to the OnCollision#### functions etc.)
    - GameObject is a container for components. Transform is the component that holds the information about the gameobjects location, orientation and scale in the scene (and it's parent/child state in the hierarchy). Since instances of GameObjects can only exist in a scene, all GameObject have a Transform component. Technically you cannot "rotate" a GameObject, you update the GameObject's Transform component with new oritentation values... but given that all GameObjects have a Transform it's just easier to think of them as pretty much the same thing.


    Should also point out those warnings aren't anything to do with that code...
     
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I think you where just missing the GetComponent

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. public class CharacterController : MonoBehaviour
    4. {
    5.     //first test for the upper leg: the objective is to control the rotation of the upper leg with a keyboard value for the rotation
    6.     // around the x-axis.
    7.     public float rotateVel = 100;
    8.     public Transform leg;
    9.     //public GameObject body;
    10.     float fowardInput, turnInput, rotationX;
    11.     void Awake()
    12.     {
    13.         //check if the leg object has been set
    14.         if(!leg)
    15.         {
    16.             //leg was not set so lets find it
    17.             Debug.Log("Leg was set, Lets find it");
    18.             leg = GameObject.Find("Upper_Leg_L").GetComponent<Transform>(); //this is fine if you only have one gmaeObject named "Upper_Leg_L" in the scene
    19.         }
    20.      
    21.     }
    22.     void Start()
    23.     {
    24.      
    25.     }
    26.  
    27.     void ModifyAngle()
    28.     {
    29.         //Obtain the leg
    30.         //leg = body.transform.Find("Upper_Leg_L"); //don't do this every frame.  it's bad for performance, do it once in Start or Awake
    31.      
    32.         //Obtain the input rotation:
    33.         rotationX = Input.GetAxis("Vertical");
    34.         rotationX *= Time.deltaTime;
    35.         leg.transform.Rotate(0, rotationX, 0);
    36.     }
    37.     void Update()
    38.     {
    39.         ModifyAngle();
    40.     }
    41. }
    There a few different type of rotate
    Transforn.rotate, transeform.rotation, eulerangles, quaternion for example
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    @johne5

    GameObject.Find returns a gameobject, and would require the addition of GetComponent<Transform> to match the variable type
    Transform.Find returns a transform
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    so he could do?
    leg = Transform.Find("Upper_Leg_L");
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    with a little t, sure. GameObject.Find searches the current scene for something (static function Find being called on the Class GameObject). referenceToSomeTransform.Find searches the referenced transform's hierarchy for something with that name (Find being called on a instance of a transform)
     
  7. MechEngi

    MechEngi

    Joined:
    Jun 4, 2018
    Posts:
    4
    Thanks a lot for the answer.
    I didn't get the difference between the method called with a uppercase or lower case.
    The answers are quite exhaustive, thanks again, seems like really handy :)
     
  8. MechEngi

    MechEngi

    Joined:
    Jun 4, 2018
    Posts:
    4
    Okay so I changed the script to include the transform.find avec t au lieu de T et j'ai mis le chemin en entier pour
    la leg :

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. public class CharacterController : MonoBehaviour
    4. {
    5.     //first test for the upper leg: the objective is to control the rotation of the upper leg with a keyboard value for the rotation
    6.     // around the x-axis.
    7.     public float rotateVel = 100;
    8.     public Transform leg;
    9.     //public GameObject body;
    10.     float fowardInput, turnInput, rotationX;
    11.     void Awake()
    12.     {
    13.         //check if the leg object has been set
    14.         if (!leg)
    15.         {
    16.             //leg was not set so lets find it
    17.             Debug.Log("Leg was set, Lets find it");
    18.             leg = transform.Find("player_weight/Armature/Hips/Upper_Leg_L").GetComponent<Transform>(); //this is fine if you only have one gmaeObject named "Upper_Leg_L" in the scene
    19.         }
    20.  
    21.     }
    22.  
    23.  
    24.     void ModifyAngle()
    25.     {
    26.         //Obtain the input rotation:
    27.         rotationX = Input.GetAxis("Vertical");
    28.         rotationX *= Time.deltaTime;
    29.         leg.transform.Rotate(0, rotationX, 0);
    30.     }
    31.     void Update()
    32.     {
    33.         ModifyAngle();
    34.     }
    35. }
    however, I have 4 remaining errors as you can see in the pics.
    Don't know what to do...
     

    Attached Files: