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