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. Dismiss Notice

Pick up a object, and walk around with it script. Item starts flying up to the air instead

Discussion in 'Scripting' started by Artonn, Jan 21, 2016.

  1. Artonn

    Artonn

    Joined:
    Jan 19, 2016
    Posts:
    7
    So i'm currently working on a script that can pick up an item, and drop it with the click of the left mouse button. I'm also planning on addint rotating of an item and some icons to display whenever i do one of those actions.

    I'm currently very new to this, so i might be trowing myself out in the depths. But i would like to try.

    Here's my code:

    Code (csharp):
    1.  
    2. public class PickUp : MonoBehaviour {
    3.  
    4.     public Transform onHand;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.         if(Input.GetMouseButton(1)){
    14.             this.transform.position = onHand.position;
    15.         }
    16.     }
    17.  
    18.     void OnMouseDown () {
    19.  
    20.  
    21.  
    22.         GetComponent<Rigidbody>().useGravity = false;
    23.         this.transform.position = onHand.position;
    24.         this.transform.parent = GameObject.Find("Player").transform;
    25.     }
    26.  
    27.     void OnMouseUp () {
    28.         this.transform.parent = null;
    29.         GetComponent<Rigidbody>().useGravity = true;
    30.     }
    31. }
    32.  
    Currently i have a gameObject, that holds the object in front of the player.

    So far it kind of works.. I've some trouble picking up my object, it does not always let me. I have to click a couple of times, before it actually gets a hold on the object. When it does, the objects starts flying upwards for some weird reason i do not understand. I still have a hold on it, i can still walk around with it and as soon as i let go it falls down. I hope you guys can help me!
     
    Last edited: Jan 21, 2016
  2. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Make sure to put the code in code blocks on the forum so that it's easy to read.

    Code (csharp):
    1.  
    2. //Like this!
    3.  
    Quote me if you want to see how to write that out.

    So then, let me look at this:


    Code (csharp):
    1.  
    2.  
    3. public class PickUp : MonoBehaviour {
    4.  
    5.     public Transform onHand;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         if(Input.GetMouseButton(1)){
    15.             this.transform.position = onHand.position;
    16.         }
    17.     }
    18.  
    19.     void OnMouseDown () {
    20.  
    21.  
    22.  
    23.         GetComponent<Rigidbody>().useGravity = false;
    24.         this.transform.position = onHand.position;
    25.         this.transform.parent = GameObject.Find("Player").transform;
    26.     }
    27.  
    28.     void OnMouseUp () {
    29.         this.transform.parent = null;
    30.         GetComponent<Rigidbody>().useGravity = true;
    31.     }
    32. }
    33.  
    EDIT: Okay, to start I wouldn't turn off gravity. That screws with the physic in unnecessary ways. Instead make a state where the object moves towards the mouse when it is clicked on and then turns off when it's done.

    I'm assuming that if you want to turn it you also want it to collide with things while you're moving is around. Correct?
     
    Last edited: Jan 21, 2016
  3. Artonn

    Artonn

    Joined:
    Jan 19, 2016
    Posts:
    7
    I'm really new to Unity, so i'm not quite sure on how to do things. I followed a tutorial i found on youtube, that is outdated now.. So I couldn't get my script to work.. Well yes i want it to be able to collide with stuff!
     
  4. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Okay, give me a second. (I'm currently not at a computer with Unity on it so I'm going to have to wing it).
     
  5. Artonn

    Artonn

    Joined:
    Jan 19, 2016
    Posts:
    7
    I've found that, if i disable gravity on my rigidbody and use Is kinematic, it stops flowing upwards. But it does not fall to the ground anymore.
     
  6. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Is this a first person game, a 2d game, or a 3d game?

    I need to know to figure out where the move the object to.

    Code (csharp):
    1.  
    2. public class PickUp : MonoBehaviour {
    3.  
    4. public Bool isPickedUp;
    5. private Rigidbody myRigidbody;
    6.  
    7. // Use this for initialization
    8.  void Start () {
    9.   //this caches the rigidbody so it doesn't have to look it up constantly
    10.   myRigidbody = GetComponent<Rigidbody>();
    11.   isPickedUp = false;
    12.  }
    13.  
    14.  void OnMouseDown() {
    15.   isPickedUp = true;
    16.  }
    17.  
    18.  void OnMouseUp() {
    19.   isPickedUp = false;
    20.  }
    21.  
    22.  //Since this is a physics movement, do it in the fixed update
    23.  void FixedUpdate() {
    24.   if (isPickedUp == true){
    25.    //Script that tells the object to move
    26.   }
    27.  }
    28.  
    29. }
    30.  
     
  7. Artonn

    Artonn

    Joined:
    Jan 19, 2016
    Posts:
    7
    This is a 3d game, the variable known as onHand is where the item is going to be positioned in the game.
     
  8. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    So when you move, does the camera move? Or is there a mouse pointer that moves around the screen?
     
  9. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Code (csharp):
    1.  
    2. public class PickUp : MonoBehaviour {
    3.  
    4. public Float moveSpeed;
    5. public Transform target;
    6. private Bool isPickedUp;
    7. private Transform myTransform;
    8.  
    9. // Use this for initialization
    10. void Start () {
    11. //this caches the transform so it doesn't have to look it up constantly
    12. myTransform = transform;
    13. isPickedUp = false;
    14. }
    15.  
    16. void OnMouseDown() {
    17. isPickedUp = true;
    18. }
    19.  
    20. void OnMouseUp() {
    21. isPickedUp = false;
    22. }
    23.  
    24. //Since this is a physics movement, do it in the fixed update
    25. void FixedUpdate() {
    26. if (isPickedUp == true){
    27. //Script that tells the object to move
    28. transform.position = Vector3.MoveTowards(myTransform.position, target.position, moveSpeed * Time.fixedDeltaTime);
    29. }
    30. }
    31.  
    32. }
    33.  
    This should move the object to what you designate as the "target" variable when you click on the object this is attached to.
     
  10. Artonn

    Artonn

    Joined:
    Jan 19, 2016
    Posts:
    7
    Tried you're code, so far you can kind of drag the object around the floor hehe.

    There's a player object, inside of the player object there is a Main Camera and a object called GameObject. Here's my firstpersoncontroller:

    Code (csharp):
    1.  
    2. [RequireComponent (typeof(CharacterController))]
    3. public class FirstPersonController : MonoBehaviour {
    4.  
    5.     //hastighed af diverse bevægelser
    6.     public float movementSpeed = 3.0f;
    7.     public float mouseSensitivity = 5.0f;
    8.     public float jumpSpeed = 20.0f;
    9.  
    10.     float verticalRotation = 0;
    11.     public float upDownRange = 60.0f;
    12.  
    13.     float verticalVelocity = 0;
    14.  
    15.     CharacterController characterController;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         //Cursor.visible = false;
    20.         //Cursor.lockState = CursorLockMode.Locked;
    21.         characterController = GetComponent<CharacterController>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.         //Rotering af spilleren,
    28.         float rotLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity;
    29.         transform.Rotate(0, rotLeftRight, 0);
    30.  
    31.         verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
    32.         verticalRotation = Mathf.Clamp (verticalRotation, -upDownRange, upDownRange);
    33.         Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
    34.  
    35.         // Bevægelse af spilleren            
    36.         float forwardSpeed = Input.GetAxis ("Vertical") * movementSpeed;
    37.         float sideSpeed = Input.GetAxis ("Horizontal") * movementSpeed;
    38.  
    39.         verticalVelocity += Physics.gravity.y * Time.deltaTime;
    40.  
    41.         if (characterController.isGrounded && Input.GetButton ("Jump")) {
    42.             verticalVelocity = jumpSpeed;
    43.         }
    44.  
    45.         Vector3 speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);
    46.         speed = transform.rotation * speed;
    47.  
    48.         characterController.Move(speed * Time.deltaTime);
    49.  
    50.  
    51.     }
    52. }
    53.  
     
  11. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    AH!

    Okay, this is a first person controller. That changes things. So you want to move things about like they do in Bethesda games.

    First, make an empty point on the character controller and place it where you want the item to move towards, such as a meter in front of the camera and 1/2 meter down.

    You're going to want to cast a ray out of the camera a short distance every frame (don't worry, it's cheap).

    Then have it check what it collides with.

    If the object is able to be picked up turn on a visual cue, such as a GUI prompt.

    If the mouse button is pressed down while it has the prompt up have it go to a "carry state" and set the item's transform to the script's cached transform.

    Move the item's transform to the empty point. You may also want to increase the movement speed as the item gets further away from the target point.

    Also, run a check to see if the distance between the player and the object is too far. If it is, turn off the carry state and drop the item.

    And of course, turn off the carry state on mouse up.



    This is actually a little bit more code, but I promise it's not that bad.

    Unfortunately I'm out of time right now, but I'll try to get back to it later.
     
  12. Artonn

    Artonn

    Joined:
    Jan 19, 2016
    Posts:
    7
    I really like how amnesia did it! I'm lost in all of those words you are saying. xD I'm quite the newbie atm, and i followed a tutorial for all i've done so far..
     
  13. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Okay, this may help:


    1) Nest an new empty game object inside the camera. Move the game object to where you are going to want to have items you pick up move to.

    2) Create a cube and change its tag to "Item" or something like that.

    3) Create a new script on the camera. And add these variables:
    -A Transform to cache the camera's position.
    -A Transform for the Empty Game Object.
    -A Transform for the item.
    -A Bool for the script's state.
    -A Float for the item's movement speed.
    -A Float for how far you want to reach.
    -A Float for how far you want to be from the object before you drop it.

    4) Shoot a ray straight out of your camera in the Update function and make it the length that you want your reach to be. About five units sounds about right.

    5) Have the ray test the tag of it's what it hits. If it's "item" have a prompt to show that you can pick it up, such as turning on an item on the GUI canvas.

    6) On Mouse Down shoot out a ray and set the Target variable to the item's transform and set a bool on so the script now knows that it is carrying an object.

    7) In the fixed update tell the item's transform to move towards the Empty Game Object if the carrying bool is true.

    8) In the update check the distance between the camera and the item. If it goes above a threshold, set the carrying bool to false.

    9) On mouse up turn off the carrying bool.