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 NaTaN08 · Jul 21, 2018 at 12:45 PM · gameobjecttransformtrigger

How to pick up objects

Hi guys I try to make a script to pick up objects, first I made this one which worked but picked up the object from anywhere:`using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PickUp : MonoBehaviour {

 public GameObject item;
 public GameObject tempParent;
 public Transform guide;

 // Use this for initialization
 void Start () {

     item.GetComponent<Rigidbody>().useGravity = true;

 }
 
 // Update is called once per frame
 void Update () {
     
 }

 void OnMouseDown()
 {
     item.GetComponent<Rigidbody>().useGravity = false;
     item.GetComponent<Rigidbody>().isKinematic = true;
     item.transform.position = guide.transform.position;
     item.transform.rotation = guide.transform.rotation;
     item.transform.parent = tempParent.transform;
 }

 void OnMouseUp()
 {
     item.GetComponent<Rigidbody>().useGravity = true;
     item.GetComponent<Rigidbody>().isKinematic = false;
     item.transform.parent = null;
     item.transform.position = guide.transform.position;
 }

} Imade a empy gameobject and addet to the player and used it as the tempParent and guide. Then I tried this one which didnt work using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Pick : MonoBehaviour {

 public GameObject item;
 public GameObject tempParent;
 public Transform guide;

 // Use this for initialization
 void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     }

 private void OnTriggerEnter ()
 {
     if(Input.GetMouseButtonDown(0)){
             item.GetComponent<Rigidbody>().useGravity = false;
             item.GetComponent<Rigidbody>().isKinematic = true;
             item.transform.position = guide.transform.position;
             item.transform.rotation = guide.transform.rotation;
             item.transform.parent = tempParent.transform;
             }

 }
 private void OnTrigerStay ()
 {
     if(Input.GetMouseButtonDown(0))
     {
         item.GetComponent<Rigidbody>().useGravity = true;
         item.GetComponent<Rigidbody>().isKinematic = false;
         item.transform.parent = null;
         item.transform.position = guide.transform.position;
         }
 }
     

} ` please reply.

Comment
Add comment · Show 4
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 Sazails · Jul 21, 2018 at 12:59 PM 0
Share

Hi, Something like this is more realistic.

     public GameObject item;
     public GameObject tempParent;
     public Transform guide;
     bool holding;
 
     private void OnTrigerStay()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             holding = !holding;
         }
         if (holding)
         {
             //Code for holding object
         }
         else
         {
                     //Code for not holding object
         }
     }

Else if you would like to pickup multiple objects I'd look into raycasting. This is more suitable for a range of objects.

avatar image NaTaN08 · Jul 21, 2018 at 01:01 PM 0
Share

@urooba ;@duck ;@aldonaletto ;@Bunny83 ;@clunk47 ;@tanoshimi

avatar image kalen_08 · Jul 21, 2018 at 01:07 PM 0
Share

what do you want the result to be?

avatar image NaTaN08 kalen_08 · Jul 23, 2018 at 05:45 PM 0
Share

I wanted to pick up the object (item) but I figured out how to do it using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : $$anonymous$$onoBehaviour { public GameObject item; public GameObject tempParent; public Transform guide; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnTriggerEnter(Collider col) { if (col.gameObject.name == "item" && Input.Get$$anonymous$$ouseButtonDown(0)) { item.GetComponent<Rigidbody>().useGravity = false; item.GetComponent<Rigidbody>().is$$anonymous$$inematic = true; item.transform.position = guide.transform.position; item.transform.rotation = guide.transform.rotation; item.transform.parent = tempParent.transform; } } void OnTriggerStay(Collider other) { if (other.gameObject.name == "item" && Input.Get$$anonymous$$ouseButtonDown(0)) { item.GetComponent<Rigidbody>().useGravity = false; item.GetComponent<Rigidbody>().is$$anonymous$$inematic = true; item.transform.position = guide.transform.position; item.transform.rotation = guide.transform.rotation; item.transform.parent = tempParent.transform; } if (other.gameObject.name == "item" && Input.Get$$anonymous$$ouseButtonUp(0)) { item.GetComponent<Rigidbody>().useGravity = true; item.GetComponent<Rigidbody>().is$$anonymous$$inematic = false; item.transform.parent = null; item.transform.position = guide.transform.position; } } }

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Liaram · Jul 23, 2018 at 06:41 PM

OnTriggerEnter() is only called in the specific frame when the collision happens. So you can't really use it for inputs. If you are clicking on objects with the mouse to pick them up you might use something like this.

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
     public GameObject handItemSlot;
     GameObject objectHit;
     Camera mainCamera;
     Rigidbody rb;
 
     void Start()
     {
         mainCamera = Camera.main;
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit))
             {
                 objectHit = hit.transform.gameObject;
 
                 //Drop any items already in the handItemSlot
                 if (handItemSlot.transform.childCount > 0)
                 {
                     foreach (Transform child in handItemSlot.transform)
                     {
                         child.transform.parent = null;
                         rb = child.GetComponent<Rigidbody>();
 
                         if (rb)
                         {
                             rb.isKinematic = false;
                             rb.useGravity = true;
                         }
                     }
                 }
 
                 //Pick up new item
                 rb = objectHit.GetComponent<Rigidbody>();
 
                 if (rb)
                 {
                     rb.isKinematic = true;
                     rb.useGravity = false;
                 }
 
                 objectHit.transform.parent = handItemSlot.transform;
                 objectHit.transform.localPosition = Vector3.zero;
                 objectHit.transform.localRotation = Quaternion.identity;
             }
         }
     }
 }
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

157 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 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

about loop transforming objects 1 Answer

What would you do with essential items? 1 Answer

My script doesnt respond what i wrote HELP!!! 0 Answers

A transform.position to act as a Vector3 1 Answer

How to move a Game Object from a script not attached to it. 1 Answer


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