Hello,
I’m making an engine building game like my summer car and my pickup script does not drop object
RaycastPickUp.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RaycastPickUp : MonoBehaviour
{
public float Range = 4f;
public LayerMask PickUp;
public Camera PlayersCamera;
public Transform ObjectHolder;
public bool ItemPickedUp;
RaycastHit hit;
Rigidbody ObjectRB;
BoxCollider ObjectBC;
void Update()
{
if(Input.GetMouseButtonDown(0) && !ItemPickedUp)
{
if(Physics.Raycast(PlayersCamera.transform.position, PlayersCamera.transform.forward, out hit, Range, PickUp))
{
ItemPickedUp = true;
CheckPickUp();
}
}
else if(Input.GetMouseButtonDown(1) && ItemPickedUp)
{
ItemPickedUp = false;
CheckPickUp();
}
}
void CheckPickUp()
{
if(Physics.Raycast(PlayersCamera.transform.position, PlayersCamera.transform.forward, out hit, Range, PickUp))
{
if(ItemPickedUp)
{
ObjectRB = hit.transform.GetComponent<Rigidbody>();
ObjectBC = hit.transform.GetComponent<BoxCollider>();
ObjectRB.useGravity = false;
ObjectBC.enabled = false;
hit.transform.SetParent(ObjectHolder);
}
else
{
ObjectRB.useGravity = true;
ObjectBC.enabled = true;
hit.transform.SetParent(null);
return;
}
}
}
}