Here’s My Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup : MonoBehaviour
{
float throwForce = 600;
Vector3 objectPos;
float distance;
float maxDistance = 3;
public bool canHold = true;
public GameObject item;
public GameObject tempParent;
public bool isHolding = false;
// Update is called once per frame
void Update()
{
distance = Vector3.Distance(item.transform.position, tempParent.transform.position);
if (distance >= 3f)
{
isHolding = false;
}
if (isHolding == true)
{
item.GetComponent<Rigidbody>().velocity = Vector3.zero;
item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
item.transform.SetParent(tempParent.transform);
if (Input.GetMouseButtonDown(1))
{
item.GetComponent<Rigidbody>().AddForce(tempParent.transform.forward * throwForce);
isHolding = false;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.Rotate(Vector3.left * 3f, Space.Self);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.Rotate(Vector3.right * 3f, Space.Self);
}
}
else
{
objectPos = item.transform.position;
item.transform.SetParent(null);
item.GetComponent<Rigidbody>().useGravity = true;
item.transform.position = objectPos;
}
}
void OnMouseOver()
{
if (distance <= 3f && Input.GetMouseButtonDown(0))
{
isHolding = true;
item.GetComponent<Rigidbody>().useGravity = false;
item.GetComponent<Rigidbody>().detectCollisions = true;
}
}
void OnMouseUp()
{
isHolding = false;
}
}
I want the same code except that the player can pickup objects when the left mouse button is CLICKED NOT HELD. (Excuse my incompetence if the answer is really simple, this is still my first week in game developement and programming in general).