Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Grabbed object going through other objects

Discussion in 'Scripting' started by theundercave, Apr 30, 2015.

  1. theundercave

    theundercave

    Joined:
    Apr 28, 2015
    Posts:
    2
    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



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GrabandDrop : MonoBehaviour {
    5.  
    6.     GameObject grabbedObject;
    7.     float grabbedObjectSize;
    8.     GameObject GetMouseHoverObject(float range)
    9.     {
    10.         Vector3 position = gameObject.transform.position;
    11.         RaycastHit raycastHit;
    12.         Vector3 target = position + Camera.main.transform.forward * range;
    13.      
    14.         if (Physics.Linecast(position, target, out raycastHit))
    15.             return raycastHit.collider.gameObject;
    16.         return null;
    17.      
    18.     }
    19.  
    20.     void TryGrabObject(GameObject grabObject)
    21.     {
    22.         if (grabObject == null || !CanGrab(grabObject))
    23.             return;
    24.         grabbedObject = grabObject;
    25.         grabbedObjectSize = grabObject.GetComponent<Renderer>().bounds.size.magnitude;
    26.     }
    27.  
    28.     bool CanGrab(GameObject canidate)
    29.     {
    30.         return canidate.GetComponent<Rigidbody> () != null;
    31.     }
    32.     void DropObject()
    33.     {
    34.         if (grabbedObject == null)
    35.             return;
    36.      
    37.      
    38.         if (grabbedObject.GetComponent<Rigidbody> () != null)
    39.             grabbedObject.GetComponent<Rigidbody> ().velocity = Vector3.zero;
    40.         grabbedObject = null;
    41.     }
    42.  
    43.     void Update () {
    44.  
    45.         if (Input.GetMouseButtonDown(0))
    46.         {
    47.             if (grabbedObject == null)
    48.                 TryGrabObject(GetMouseHoverObject(5));
    49.             else
    50.                 DropObject();
    51.         }
    52.      
    53.         if (grabbedObject != null)
    54.         {
    55.             Vector3 newPosition = gameObject.transform.position+Camera.main.transform.forward*grabbedObjectSize;
    56.             grabbedObject.transform.position = newPosition;
    57.         }
    58.      
    59.     }
    60. }
     
    Last edited: Apr 30, 2015
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    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.
     
    ThermalFusion likes this.
  4. theundercave

    theundercave

    Joined:
    Apr 28, 2015
    Posts:
    2
    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?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  6. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    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.