Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

HELP!

Discussion in 'Scripting' started by limejellodragon, Jul 13, 2018.

  1. limejellodragon

    limejellodragon

    Joined:
    Jun 14, 2018
    Posts:
    3
    So I'm creating a gun pickup script in C#, which the basic goal is to be able to walk up and pick up the object and drop it too, and I'm currently testing out and trying to create a script. I'm using the RigidBodyFirstPersonController and have three cubes, two of which have the "pickupobject" tag. I have two issues. One is while I'm holding one cube, if the pointer goes across the second cube, everything gets messed up and I cant drop the first cube. The second issue is that the cubes float off into space while I hold them, they aren't being locked into place, the only way they move is when I move the mouse around. I'm pretty new to Unity and C# so any help would be appreciated. Thanks!

    Here's my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Raycasting : MonoBehaviour {
    GameObject testObj;
    bool canHover = false;
    public Transform onhand;
    public bool isHolding;
    private void Update()
    {
    var forward = transform.TransformDirection(Vector3.forward);
    RaycastHit hit;
    if (Physics.Raycast(transform.position, forward, out hit))
    {
    if (hit.distance <= 5.0f && hit.collider.gameObject.tag == "pickupobject")
    {
    canHover = true;
    testObj = hit.transform.gameObject;
    if (isHolding == true)
    {
    testObj.GetComponent<Rigidbody>().useGravity = false;
    testObj.transform.position = onhand.position;
    testObj.transform.parent = GameObject.Find("RigidBodyFPSController").transform;
    testObj.transform.parent = GameObject.Find("MainCamera").transform;
    Debug.Log(testObj.name);
    isHolding = true;
    }
    if (Input.GetButtonDown("e"))
    {
    isHolding = true;
    }
    if (isHolding == true && Input.GetButtonDown("e"))
    {
    testObj.transform.parent = null;
    testObj.GetComponent<Rigidbody>().useGravity = true;
    isHolding = false;
    }
    }
    else
    {
    canHover = false;
    }
    }
    }
    private void OnGUI()
    {
    if (canHover == true)
    {
    GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 150, 20), "Pick Me Up");
    }
    }
    }
     
    Last edited: Jul 13, 2018
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,822
    Ok, my first tip. Use code tags when posting code.

    Second...there are things that look wrong in the script, but because it's hard to line up brackets, I can't tell where your code scopes are (thus the need for tags)

    However, just to answer your issue. My guess is you are picking up another item and not checking if you are already holding an item or not dropping the current item if you have something and then picking up the new item.
     
  3. F14M3THR0W3R

    F14M3THR0W3R

    Joined:
    Aug 18, 2013
    Posts:
    61
    Please use code tags, I'm hurting my eyes looking at that code:
    (https://forum.unity.com/threads/using-code-tags-properly.143875/)
    Also, the title is a bit immature. I suggest you change it to something reasonable ;)

    Alright so just from looking at the code, it seems like you want to pick up the gun when the player is looking at the gun?
    If that's the case, then your casting a ray in the wrong direction. Instead of:
    var forward = transform.TransformDirection(Vector3.forward);

    It should be something like this:
    var forward = transform.TransformDirection(playerCamera.transform.forward);


    Also, in these lines:
    Code (CSharp):
    1.     testObj.transform.parent = GameObject.Find("RigidBodyFPSController").transform;
    2. testObj.transform.parent = GameObject.Find("MainCamera").transform;
    3.  
    I don't understand why you're setting the parent once, then again, there is no point of setting the parent twice, one after the other.
    Also, you should always avoid calling GameObject.Find in the Update function, it's much better practice to save the reference in the Start function like this:
    Code (CSharp):
    1. GameObject mainCam;
    2.  
    3. void Start () {
    4.     mainCam = GameObject.Find("MainCamera");
    5. }
    And then when setting the parent do this:
    Code (CSharp):
    1. testObj.transform.parent = mainCam.transform;
    Let me know if you want me to make something clearer.

    For those who would actually like to read his code, here it is properly formatted:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Raycasting : MonoBehaviour {
    6.     GameObject testObj;
    7.     bool canHover = false;
    8.     public Transform onhand;
    9.     public bool isHolding;
    10.  
    11.     private void Update()
    12.     {
    13.         var forward = transform.TransformDirection(Vector3.forward);
    14.         RaycastHit hit;
    15.    
    16.         if (Physics.Raycast(transform.position, forward, out hit))
    17.         {
    18.             if (hit.distance <= 5.0f && hit.collider.gameObject.tag == "pickupobject")
    19.             {
    20.                 canHover = true;
    21.                 testObj = hit.transform.gameObject;
    22.            
    23.                 if (isHolding == true)
    24.                 {
    25.                     testObj.GetComponent<Rigidbody>().useGravity = false;
    26.                     testObj.transform.position = onhand.position;
    27.                     testObj.transform.parent = GameObject.Find("RigidBodyFPSController").transform;
    28.                     testObj.transform.parent = GameObject.Find("MainCamera").transform;
    29.                     Debug.Log(testObj.name);
    30.                     isHolding = true;
    31.                 }
    32.            
    33.                 if (Input.GetButtonDown("e"))
    34.                 {
    35.                     isHolding = true;
    36.                 }
    37.            
    38.                 if (isHolding == true && Input.GetButtonDown("e"))
    39.                 {
    40.                     testObj.transform.parent = null;
    41.                     testObj.GetComponent<Rigidbody>().useGravity = true;
    42.                     isHolding = false;
    43.                 }
    44.             }
    45.             else
    46.             {
    47.                 canHover = false;
    48.             }
    49.         }
    50.     }
    51.    
    52.     private void OnGUI()
    53.     {
    54.         if (canHover == true)
    55.         {
    56.             GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 150, 20), "Pick Me Up");
    57.         }
    58.     }
    59. }