I just followed a tutorial on this physics based pick up script, but for some reason the item im picking up doesnt move with my character, only with my mouse movements. for example if i move side to side and not move my mouse, the item will just stay where its at. and the only time it moves is with my camera movement.
using JetBrains.Annotations;
using UnityEngine;
public class PickupObject : MonoBehaviour
{
public float pickUpForce = 150f;
public float pickUpRange = 5f;
public LayerMask isPickable;
private GameObject heldObj;
private Rigidbody heldObjRB;
public Transform holdArea;
public Transform player;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
private void Update()
{
if(Input.GetMouseButtonDown(0))
{
if(heldObj == null)
{
RaycastHit hit;
if(Physics.Raycast(transform.position,transform.forward, out hit, pickUpRange))
{
Pickup(hit.transform.gameObject);
}
}
else
{
Drop();
}
if(heldObj != null)
{
MoveObject();
}
}
}
void Pickup(GameObject pickObj)
{
if(pickObj.GetComponent<Rigidbody>())
{
heldObjRB = pickObj.GetComponent<Rigidbody>();
heldObjRB.useGravity = false;
heldObjRB.linearDamping = 5;
heldObjRB.constraints = RigidbodyConstraints.FreezeRotation;
heldObjRB.transform.parent = holdArea;
heldObj = pickObj;
}
}
void MoveObject()
{
if(Vector3.Distance(heldObj.transform.position, holdArea.position) > 0.1f)
{
Vector3 moveDirection = holdArea.position - heldObj.transform.position;
heldObjRB.AddForce(moveDirection * pickUpForce);
}
}
void Drop()
{
heldObjRB.useGravity = true;
heldObjRB.linearDamping = 1;
heldObjRB.constraints = RigidbodyConstraints.None;
heldObjRB.transform.parent = null;
heldObj = null;
}
}