- Home /
Grab script errors
My Grab script allows me to move the object but when I collide it into a wall it goes threw the wall and whenever I disable the box collider the item just flys away when I hit it to the wall and I cant drop the item can anyone help me
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUp : MonoBehaviour
{
public Transform theDest;
void OnMouseDown()
{
GetComponent<BoxCollider>().enabled = false;
GetComponent<Rigidbody>().useGravity = false;
this.transform.position = theDest.position;
this.transform.parent = GameObject.Find("Destination").transform;
}
void onMouseUp()
{
this.transform.parent = null;
GetComponent<Rigidbody>().useGravity = true;
GetComponent<BoxCollider>().enabled = true;
}
}
Comment
Answer by Spip5 · Aug 12, 2020 at 11:55 PM
@Cane1060 As per the documentation, OnMouseDown / Up need the collider to trigger, so that is normal you cannot grab it again after disabling the object's collider.
The best solution will vary based on your game (FPS, TPS, etc...) but i'd suggest you use raycasts instead of OnMouseDown, which is limited by the colider. Maybe try to put the collider on Trigger mode ? never tried myself but i guess it should work.
// Mouse down
GetComponent<BoxCollider>().isTrigger= true;
// Mouse up
GetComponent<BoxCollider>().isTrigger= false;