- Home /
How can i find a thirdpersoncontroller hand and attach object to the hand ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class Flashlight : MonoBehaviour {
public GameObject playerToAttach;
// Use this for initialization
void Start ()
{
var someBone = playerToAttach.transform.Find("LeftShoulder/Arm/Hand");
transform.parent = someBone;
transform.localPosition = someBone.position;
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
}
// Update is called once per frame
void Update () {
}
}
I attach the script to a cube. I want to attach the cube to the ThirdPersonController hand. So i dragged the ThirdPersonController to the inspector to the script in the cube.
But the variable someBone is null all the time. playerToAttach is not null. I guess doing: Find("LeftShoulder/Arm/Hand"); is wrong way.
Also the rest of the code is wrong i guess:
transform.localPosition = someBone.position;
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
What i want to do is to attach an object to the thirdpersoncontroller hand. Like he is holding the object.
why don't you try to make the hand the parent object of the cube? and adjust the cube transform manually on the inspector?
I did but now the flashlight is floating near the hand. How can i make that the character will hold the flashlight in his hand palm ?
I changed the GameObject position to the hand position but it's not holding it it's just floating near the character. Look at the screenshot i added.
And i did it all in the Hierarchy i'm not using yet any script. I wonder how to make it to hold the flashlight with the hand palm.
I'm sorry the screenshot didn't show up can you post it again?
Answer by spooneystone · Apr 27, 2017 at 08:13 AM
Best to try this maybe
public class Flashlight : MonoBehaviour {
public GameObject playerToAttach;
[SerielizeField]
Transform someBone;
// Use this for initialization
void Start ()
{
transform.parent = someBone;
}
// Update is called once per frame
void Update () {
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
}
}
Then just Drag the "someBone" object in the inspector. Also moved the localRotation and scale to "update" as it wont update every frame. No need to set the transform.position because its been set to its parent so it will follow that object anyway.
Ok i tried this. every bone i tried to drag to the inspector to the script to the someBone is not the the: hand palm. It's always near it. It doesn't looks like he is holding it with the hand palm that's the problem. It's fine with the script and working fine when i drag the bones but i can't find the hand palm.