Question by
gripingberry · Jul 31, 2016 at 12:19 PM ·
c#transforminventory
Move Object to Players Hand
I have the following code: public class InteractionManager : MonoBehaviour {
public GameObject arms;
private Animator anim;
public float grabDist = 3;
static int grabState = Animator.StringToHash("Base.grab");
public GameObject holder;
// Use this for initialization
void Start () {
anim = arms.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
anim.SetTrigger("grab");
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, grabDist)) {
if (hit.collider.tag.Equals("Interactable")) {
GameObject obj = hit.collider.gameObject;
GameObject objParent = obj.transform.parent.gameObject;
//objParent.SetActive(false);
obj.GetComponent<Collider>().enabled = false;
obj.GetComponent<Rigidbody>().isKinematic = false;
objParent.transform.parent = holder.transform;
//StartCoroutine(FinishGrab());
objParent.SetActive(true);
objParent.transform.localPosition = Vector3.zero;
objParent.transform.Rotate(0, 0, 0);
obj.layer = 8;
anim.SetBool("lantern", true);
}
}
}
}
IEnumerator FinishGrab() {
yield return new WaitUntil(() => !(anim.GetCurrentAnimatorStateInfo(0).fullPathHash == grabState));
}
}
It is intended to allow me to pick up a lantern by having an empty object that it is a child of get moved to align with an empty object hovering the players hand. When I try this the lantern moves but then just starts falling out of the world. Any help is appreciated.
Comment
Your answer
