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

Question Is This Possible

Discussion in 'Scripting' started by ShaunChad99, May 11, 2020.

  1. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    I want to make a script where i can use my "PickUpObject" script and move them into the back of a vehicle and store them there untill n take them out using the "PickUpObject" script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PickUpObject : MonoBehaviour
    6. {
    7.     public Transform theDest;
    8.  
    9.     public Player Player;
    10.  
    11.     void Update()
    12.     {
    13.          
    14.     }
    15.  
    16.  
    17.     void OnMouseDown()
    18.     {
    19.         GetComponent<BoxCollider>().enabled = false;
    20.         GetComponent<Rigidbody>().useGravity = false;
    21.         this.transform.position = theDest.position;
    22.         this.transform.parent = GameObject.Find("Item Dest").transform;
    23.     }
    24.     void OnMouseUp()
    25.     {
    26.         GetComponent<BoxCollider>().enabled = true;
    27.         this.transform.parent = null;
    28.         GetComponent<Rigidbody>().useGravity = true;
    29.     }
    30. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    This is a great start, but you might want to review some tutorials on picking up stuff and dropping it. There's a lot of other subtle details omitted from the above code, such as moving it to the right place once it is picked up and parented, or placing it back into the world in a way that is reasonable and keeps it from falling through other objects or falling through the floor.

    Also, this script appears mildly confused between if it is doing the picking up of another object that was clicked, or it assumes it is the thing itself that is clicked. Review how that is set up in some of the tutorials.
     
  3. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    Okay i will do thank you