- Home /
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.
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.
@urooba ;@duck ;@aldonaletto ;@Bunny83 ;@clunk47 ;@tanoshimi
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; } } }
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;
}
}
}
}