Pick Up and Drop Script not functioning due to loophole
Hello, this is a script i wrote designed to pick up objects and drop when mouse button is released. The problem is, when pick up an object and it bumps against another object it is no longer to the destination I want which is the center of the screen. So I implemented that when the object is not being touched it resume its destination at the center of the screen. But when the script checks is it being it touched it checks the ground so the object doesn't pick up. I don't really know how to get around this so any help would be appreciated. Thanks. using System.Collections; using System.Collections.Generic; using UnityEngine; public class PickUp : MonoBehaviour { private bool hasPlayer; public Transform theDest; public Transform player; private bool beingCarried = false; private bool touched = false; void Update() { float dist = Vector3.Distance(gameObject.transform.position, player.position); if (dist <= 6f) { hasPlayer = true; } else { hasPlayer = false; } } void OnMouseDown() { void OnTriggerEnter() { if (beingCarried) { touched = true; } } void Update() { if (hasPlayer == true) { if (touched = false) { GetComponent<Rigidbody>().useGravity = false; GetComponent<Rigidbody>().freezeRotation = true; this.transform.position = theDest.position; this.transform.parent = GameObject.Find("Destination").transform; beingCarried = true; } } } } void OnMouseUp() { this.transform.parent = null; GetComponent<Rigidbody>().useGravity = true; GetComponent<Rigidbody>().freezeRotation = false; } }
Answer by streeetwalker · Mar 22, 2020 at 10:46 AM
@christophermyoung05 , the problem, it seems, is you want to drag and drop objects while the physics engine is in control, so it is reacting to other objects in the scene? Why not just temporarily deactivate the physics and collider on the object you want to drag, and then reactivate on drop?
Your answer
Follow this Question
Related Questions
How to fix this problem? 1 Answer
Rigidbody randomly going through collider 1 Answer
Freezing an object and keeping it's velocity 1 Answer
Making a pivot focused game 0 Answers
AddRigidBody Script Error 1 Answer