- Home /
AI pick object
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: http://answers.unity3d.com/questions/535406/how-to-grab-pick-and-place-an-object-to-other-loca.html
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);
}
}
}
}`
sounds like you have to move the object to the hands position. or figure how to get the hand closer to the model before parenting.
Answer by webcam · May 16, 2017 at 02:33 AM
Hey mate, so what you should be trying to do is move your hand all the way to the item and then parent it.
While you're running your IK you could check the distance between the right hand position and the rightHandObj, when the distance is close to zero you can use your
rightHandObj.transform.SetParent(GameObject.FindWithTag("RightHand").transform, false
If it's not sitting in the hand to your liking you can then set the position of the object with something like
rightHandObj.transform.localPosition = Vector3.zero
Or some other local position that fits the hand better. If you set the local position to zero and its still offset it probably means your mesh is a child with an offset or something similar.
Also I would cache that right hand transform instead of finding by tag every time.
Answer by IMemeManI · May 18, 2017 at 11:06 AM
transform.localPosition = new Vector3(0, 0, 0);
That's a possible solution. Also, has your AI got a right hand mesh? If so, apply the script to that and not the parent. I'm still learning C# so sorry if this didn't help. But i'm sure you have to transform it's position on the x,y and z axis to 0, or if you wanted it fine tuned to fit the hand, use, transform.localroation
?
Best wait till an expert comes, but give those lines of code a google search, you might find something.
Your answer
![](https://koobas.hobune.stream/wayback/20220612122054im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What am I doing wrong with my AI? 1 Answer
AI Case Switch Issue - Javascript, 2D Shooter 1 Answer
Mechanim - should be simple, but... 2 Answers
Programming an enemy to shoot upwards 1 Answer
Error with Rotating script 2 Answers