Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Eco-Editor · May 15, 2017 at 07:48 PM · transformaiavatarikpick up object

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);
         }
     }
 }
 }`  



Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · May 16, 2017 at 01:03 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges