Search Unity

Pick up and carry objects [Problem]

Discussion in 'Getting Started' started by Rukas90, Nov 4, 2016.

  1. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    169
    I have one problem with my pick up and carry objects script. The problem is, the objects can go through the walls (other objects). Is there any way to fix this problem? Thank you!

    The script:
    Code (JavaScript):
    1. var onhand : Transform;
    2. public var Distance : float;
    3. public var pickUp_Distance : float;
    4. var player : Transform;
    5.  
    6. function Update ()
    7. {
    8.  
    9. }
    10.  
    11. function OnMouseDown ()
    12. {
    13.     Distance = Vector3.Distance(player.position, this.transform.position);
    14.     if(Distance <= pickUp_Distance)
    15.     {
    16.         GetComponent.<Rigidbody>().useGravity = false;
    17.         GetComponent.<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
    18.         this.transform.position = onhand.position;
    19.         this.transform.rotation = Quaternion.identity;
    20.         this.transform.parent = GameObject.Find("Player").transform;
    21.         this.transform.parent = GameObject.Find("Main Camera").transform;
    22.     }
    23. }
    24.  
    25. function OnMouseUp ()
    26. {
    27.     this.transform.parent = null;
    28.     GetComponent.<Rigidbody>().constraints = RigidbodyConstraints.None;
    29.     GetComponent.<Rigidbody>().useGravity = true;
    30.  
    31. }