Hello all,
I have avatars in the scene and they walk around a supermarket and pick up an object once enter a collider.
Following this answer: How to grab, pick and place an object to other location using Mecanim IK Script - Questions & Answers - Unity Discussions
and a script (below), I’ve managed to get the avatar to pick the object and make the object a child of the avatars’ hand. BUT even though the object is a child of the avatars hand, it’s not in it’s hand, but it’s somewhere near the hand following the avatar everywhere. How do I solve this problem? Can I give a function that resets the objects transform once it’s at avatars hand?
I’ve already used:
Use this .transform.SetParent(this.gameObject.transform,false);
But it didn’t work for me.
Here’s the code in C#:
using UnityEngine;
using System;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class IKControl : MonoBehaviour
{
protected Animator animator;
public bool ikActive = false;
public Transform rightHandObj = null;
public Transform lookObj = null;
void Start()
{
ikActive = false;
animator = GetComponent<Animator>();
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Product")
{
ikActive = true;
}
}
public void OnAnimatorIK()
{
if (animator)
{
if (ikActive)
{
if (lookObj != null)
{
animator.SetLookAtWeight(1);
animator.SetLookAtPosition(lookObj.position);
}
if (rightHandObj != null)
{
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
}
rightHandObj.transform.SetParent(GameObject.FindWithTag("RightHand").transform, false);
}
else
{
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 0);
animator.SetLookAtWeight(0);
}
}
}
}`