Grabbed object going through other objects

Hello, I would first like to say that I am fairly new to the unity environment, anyways, I have this c# script that lets me pick up objects and put them down, which is awesome and it works, the only problem is, the object that you’re holding can go through other objects, the terrain , other objects ect. I’m not sure what to do? Could anyone offer me some help? here is my code for my GrabAndDrop script

using UnityEngine;
using System.Collections;

public class GrabandDrop : MonoBehaviour {
 
    GameObject grabbedObject;
    float grabbedObjectSize;
    GameObject GetMouseHoverObject(float range)
    {
        Vector3 position = gameObject.transform.position;
        RaycastHit raycastHit;
        Vector3 target = position + Camera.main.transform.forward * range;
     
        if (Physics.Linecast(position, target, out raycastHit))
            return raycastHit.collider.gameObject;
        return null;
     
    }
 
    void TryGrabObject(GameObject grabObject)
    {
        if (grabObject == null || !CanGrab(grabObject))
            return;
        grabbedObject = grabObject;
        grabbedObjectSize = grabObject.GetComponent<Renderer>().bounds.size.magnitude;
    }

    bool CanGrab(GameObject canidate)
    {
        return canidate.GetComponent<Rigidbody> () != null;
    }
    void DropObject()
    {
        if (grabbedObject == null)
            return;
     
     
        if (grabbedObject.GetComponent<Rigidbody> () != null)
            grabbedObject.GetComponent<Rigidbody> ().velocity = Vector3.zero;
        grabbedObject = null;
    }
 
    void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            if (grabbedObject == null)
                TryGrabObject(GetMouseHoverObject(5));
            else
                DropObject();
        }
     
        if (grabbedObject != null)
        {
            Vector3 newPosition = gameObject.transform.position+Camera.main.transform.forward*grabbedObjectSize;
            grabbedObject.transform.position = newPosition;
        }
     
    }
}

Please use code tags: Using code tags properly - Unity Engine - Unity Discussions

The reason the grabbed object can go through other objects is because while you’re grabbing it, you’re changing its transform directly each frame. This overrides whatever would be calculated and affected by the physics engine.

You’d have to either a) do a physics sweep to check for potential collisions before changing the transform, or b) use AddForce instead to affect the object’s position.

1 Like

Sorry about the awfully late reply, but how do I go about doing this? Or anything I should look up that will relate to my project?

covers the basic “physics” movement ideas.

1 Like

The new Standard Assets has a script called DragRigidbody which lets you, surprisingly enough, drag a rigidbody around by forces while still letting it interact properly with the environment. You should be able to tailor it to work for your needs.