Search Unity

Pick Up object in Online Game

Discussion in 'Multiplayer' started by Barrufy96, Jul 15, 2018.

  1. Barrufy96

    Barrufy96

    Joined:
    Jun 30, 2018
    Posts:
    9
    Guys, I am developing a project for university and I need my player to pick up an object (let's say a box).

    I have attatched to my player prefab a Network Identity and a Network Transform, and the same to my GameObject box that has to be picked up.

    With the script I was trying to make the player pickup the object and then be able to move around, but when I click on the box it goes to the previous prefab position.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class MoveObject : NetworkBehaviour {
    7.  
    8.     public GameObject item;
    9.     public GameObject tempParent;
    10.     public Transform guide;
    11.     // Use this for initialization
    12.     void Start () {
    13.         item.GetComponent<Rigidbody>().useGravity = true;
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.        
    19.     }
    20.  
    21.     void OnMouseDown()
    22.     {
    23.         item.GetComponent<Rigidbody>().useGravity = false;
    24.         item.GetComponent<Rigidbody>().isKinematic = true;
    25.         item.transform.position = guide.transform.position;
    26.         item.transform.rotation = guide.transform.rotation;
    27.  
    28.     }
    29.  
    30.     void OnMouseUp()
    31.     {
    32.         item.GetComponent<Rigidbody>().useGravity = true;
    33.         item.GetComponent<Rigidbody>().isKinematic = false;
    34.         item.transform.parent = null;
    35.         item.transform.position = guide.transform.position;
    36.        
    37.     }
    38. }
    39.  
    The guide is a child empty object put right in front of the player prefab (so that it looks like it is holding the box).

    Any suggestions?
     
  2. crydrk

    crydrk

    Joined:
    Feb 10, 2012
    Posts:
    74
    According to the code here, you're only moving item to guide's position on click, and then doing nothing else until mouse up. Did you want to make item a child of maybe guide or tempParent (whichever item is moving elsewhere in code)? If so looks like you forgot to set the parent in OnMouseDown.

    Also, this isn't affecting your problem, but you can just use guide.position, since guide is a Transform.
     
  3. Barrufy96

    Barrufy96

    Joined:
    Jun 30, 2018
    Posts:
    9
    My aim is to make the player take the box on mouse down and then let it go on mouse up, while it can move around while holding the box.

    I am quite noob in this, how can I set the parent in OnMouseDown?

    Thanks
     
  4. crydrk

    crydrk

    Joined:
    Feb 10, 2012
    Posts:
    74
    You would set the parent similar to the way you're un-parenting in OnMouseUp, except the way you're doing it currently is deprecated. Transform.SetParent(whateverTransform) is the proper function for this.

    For my explanation here, I'm going to assume tempParent is the object you want to parent the item under.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. public class MoveObject : NetworkBehaviour {
    6.     public GameObject item;
    7.     public GameObject tempParent;
    8.     public Transform guide;
    9.     // Use this for initialization
    10.     void Start () {
    11.         item.GetComponent<Rigidbody>().useGravity = true;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.    
    17.     }
    18.  
    19.     void OnMouseDown()
    20.     {
    21.         item.GetComponent<Rigidbody>().useGravity = false;
    22.         item.GetComponent<Rigidbody>().isKinematic = true;
    23.      
    24.         item.transform.SetParent(tempParent);
    25.  
    26.         // This is pretty much the same as what you were doing setting the position, but I just
    27.         // prefer to set the parent first, the zero out the position and rotation local to the parent
    28.         item.transform.localPosition = Vector3.zero;
    29.         item.transform.localRotation = Quaternion.identity;
    30.     }
    31.  
    32.     void OnMouseUp()
    33.     {
    34.         item.GetComponent<Rigidbody>().useGravity = true;
    35.         item.GetComponent<Rigidbody>().isKinematic = false;
    36.  
    37.         // item.transform.parent = null; <- your old version of setting parent to null
    38.         item.transform.SetParent(null); // <- correct way
    39.      
    40.         //item.transform.position = guide.transform.position; // I've remove this line, because this is what's causing it to snap back
    41.    
    42.     }
    43. }
    44.  
     
    Last edited: Jul 18, 2018
    Barrufy96 likes this.
  5. Barrufy96

    Barrufy96

    Joined:
    Jun 30, 2018
    Posts:
    9
    You're awesome, thanks!