using UnityEngine;
public class Grabbing : MonoBehaviour
{
[SerializeField] float maxDistance;
[SerializeField] float GrapPower;
[Space]
[SerializeField] Transform Offset;
Transform GrabObj;
RaycastHit hit;
bool grab;
private void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Mouse0) || grab)
{
Grab();
}
if (Input.GetKeyDown(KeyCode.E) && grab)
{
Put();
}
}
private void Grab()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit, maxDistance);
if(grab)
{
GrabObj.position = Offset.position;
}
else if(hit.rigidbody)
{
grab = true;
hit.rigidbody.useGravity = false;
Offset.position = hit.transform.position;
GrabObj = hit.transform;
}
}
private void Put()
{
GrabObj.GetComponent<Rigidbody>().useGravity = true;
grab = false;
}
}