Object clipping while using pickup script
So I got a pickup script. When i pick an object up it and i try to push it, it clips through the wall. Any suggestions on how to fix? (Reference video)
https://reddit.com/link/1fcsd8q/video/3q546mz8zsnd1/player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupController : MonoBehaviour
{
[Header("Setup")]
[SerializeField] Transform holdArea;
private GameObject heldObject;
private Rigidbody heldObjectRigidbody;
[Header("Physics")]
[SerializeField] private float pickupRange = 5.0f;
[SerializeField] private float pickupForce = 150.0f;
private void Update() {
if (Input.GetMouseButtonDown(0))
{
if (heldObject == null)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, pickupRange))
{
PickupObject(hit.transform.gameObject);
}
}
else
{
DropObject();
}
}
if(heldObject != null)
{
MoveObject();
}
}
void MoveObject()
{
if (Vector3.Distance(heldObject.transform.position, holdArea.position) > 0.1f)
{
Vector3 moveDirection = (holdArea.position - heldObject.transform.position);
heldObjectRigidbody.AddForce(moveDirection * pickupForce);
}
}
void PickupObject(GameObject pickObject)
{
if (pickObject.GetComponent<Rigidbody>())
{
heldObjectRigidbody = pickObject.GetComponent<Rigidbody>();
heldObjectRigidbody.useGravity = false;
heldObjectRigidbody.drag = 10;
heldObjectRigidbody.constraints = RigidbodyConstraints.FreezeRotation;
heldObjectRigidbody.transform.parent = holdArea;
heldObject = pickObject;
}
}
void DropObject()
{
heldObjectRigidbody.useGravity = true;
heldObjectRigidbody.drag = 1;
heldObjectRigidbody.constraints = RigidbodyConstraints.None;
heldObjectRigidbody.transform.parent = null;
heldObject = null;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupController : MonoBehaviour
{
[Header("Setup")]
[SerializeField] Transform holdArea;
private GameObject heldObject;
private Rigidbody heldObjectRigidbody;
[Header("Physics")]
[SerializeField] private float pickupRange = 5.0f;
[SerializeField] private float pickupForce = 150.0f;
private void Update() {
if (Input.GetMouseButtonDown(0))
{
if (heldObject == null)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, pickupRange))
{
PickupObject(hit.transform.gameObject);
}
}
else
{
DropObject();
}
}
if(heldObject != null)
{
MoveObject();
}
}
void MoveObject()
{
if (Vector3.Distance(heldObject.transform.position, holdArea.position) > 0.1f)
{
Vector3 moveDirection = (holdArea.position - heldObject.transform.position);
heldObjectRigidbody.AddForce(moveDirection * pickupForce);
}
}
void PickupObject(GameObject pickObject)
{
if (pickObject.GetComponent<Rigidbody>())
{
heldObjectRigidbody = pickObject.GetComponent<Rigidbody>();
heldObjectRigidbody.useGravity = false;
heldObjectRigidbody.drag = 10;
heldObjectRigidbody.constraints = RigidbodyConstraints.FreezeRotation;
heldObjectRigidbody.transform.parent = holdArea;
heldObject = pickObject;
}
}
void DropObject()
{
heldObjectRigidbody.useGravity = true;
heldObjectRigidbody.drag = 1;
heldObjectRigidbody.constraints = RigidbodyConstraints.None;
heldObjectRigidbody.transform.parent = null;
heldObject = null;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupController : MonoBehaviour
{
[Header("Setup")]
[SerializeField] Transform holdArea;
private GameObject heldObject;
private Rigidbody heldObjectRigidbody;
[Header("Physics")]
[SerializeField] private float pickupRange = 5.0f;
[SerializeField] private float pickupForce = 150.0f;
private void FixedUpdate() {
if (Input.GetMouseButtonDown(0))
{
if (heldObject == null)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, pickupRange))
{
PickupObject(hit.transform.gameObject);
}
}
else
{
DropObject();
}
}
if(heldObject != null)
{
MoveObject();
}
}
void MoveObject()
{
if (Vector3.Distance(heldObject.transform.position, holdArea.position) > 0.1f)
{
Vector3 moveDirection = (holdArea.position - heldObject.transform.position);
heldObjectRigidbody.AddForce(moveDirection * pickupForce * Time.fixedDeltaTime);
}
}
void PickupObject(GameObject pickObject)
{
if (pickObject.GetComponent<Rigidbody>())
{
heldObjectRigidbody = pickObject.GetComponent<Rigidbody>();
heldObjectRigidbody.useGravity = false;
heldObjectRigidbody.drag = 10;
heldObjectRigidbody.constraints = RigidbodyConstraints.FreezeRotation;
heldObjectRigidbody.transform.parent = holdArea;
heldObject = pickObject;
}
}
void DropObject()
{
heldObjectRigidbody.useGravity = true;
heldObjectRigidbody.drag = 1;
heldObjectRigidbody.constraints = RigidbodyConstraints.None;
heldObjectRigidbody.transform.parent = null;
heldObject = null;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupController : MonoBehaviour
{
[Header("Setup")]
[SerializeField] Transform holdArea;
private GameObject heldObject;
private Rigidbody heldObjectRigidbody;
[Header("Physics")]
[SerializeField] private float pickupRange = 5.0f;
[SerializeField] private float pickupForce = 150.0f;
private void FixedUpdate() {
if (Input.GetMouseButtonDown(0))
{
if (heldObject == null)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, pickupRange))
{
PickupObject(hit.transform.gameObject);
}
}
else
{
DropObject();
}
}
if(heldObject != null)
{
MoveObject();
}
}
void MoveObject()
{
if (Vector3.Distance(heldObject.transform.position, holdArea.position) > 0.1f)
{
Vector3 moveDirection = (holdArea.position - heldObject.transform.position);
heldObjectRigidbody.AddForce(moveDirection * pickupForce * Time.fixedDeltaTime);
}
}
void PickupObject(GameObject pickObject)
{
if (pickObject.GetComponent<Rigidbody>())
{
heldObjectRigidbody = pickObject.GetComponent<Rigidbody>();
heldObjectRigidbody.useGravity = false;
heldObjectRigidbody.drag = 10;
heldObjectRigidbody.constraints = RigidbodyConstraints.FreezeRotation;
heldObjectRigidbody.transform.parent = holdArea;
heldObject = pickObject;
}
}
void DropObject()
{
heldObjectRigidbody.useGravity = true;
heldObjectRigidbody.drag = 1;
heldObjectRigidbody.constraints = RigidbodyConstraints.None;
heldObjectRigidbody.transform.parent = null;
heldObject = null;
}
}