Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by RVDL_IT · May 13, 2017 at 04:46 PM · c#bugbugs

Bugs in a pick up script

I have a couple of bugs that I can't seem to fix:

  1. When I try to pick up an item I pick up all of the items that my script is attached to (and that are supposed to be able to be picked up).

  2. The gravity doesn't turn off when I try to pick up an item.

  3. I can't pick up 1 of the items that this script is attached to (, I can pick up the other ones work though).

  4. The colliders on the children of the items don't turn off when I try to pick up the item.

The PickUpItem() and DropItem() voids are called in Update() when you press the E key.

Any help is appreciated.

This is my script:

     using UnityEngine;
     using System.Collections;
     
     namespace CarryingItems {
     
         public class PickUp : MonoBehaviour {
               
             public float RayCastLength = 2.15F;
             public LayerMask RayMask;
                  
             public float OffsetDropPosition = 2;
          
             private GameObject Handle;
             private GameObject HandleExtra;
             private GameObject BladeHolder;
             private GameObject Blade;
             private GameObject CameraMain;
             public GameObject CarryableObject;
             private GameObject Character;
             private GameObject UtilityProps;
              
             private bool ItemPickedUp;
                  
             void Awake() {
                 CameraMain = GameObject.Find("MainCamera");
                 Character = GameObject.Find("Character");
                 UtilityProps = GameObject.Find("UtilityProps");
                 Handle = this.gameObject.transform.GetChild(0).gameObject;
                 HandleExtra = this.gameObject.transform.GetChild(1).gameObject;
                 BladeHolder = this.gameObject.transform.GetChild(2).gameObject;
                 Blade = this.gameObject.transform.GetChild(3).gameObject;
             }     
          
             void Start() {
                 ItemPickedUp = false;
             }
          
             void Update() {
                 if (Input.GetKeyDown("e")) {
                     ItemPickUp();
                     ItemDrop();
                 } 
             }
          
             void ItemPickUp() {
                 if(ItemPickedUp == false) {
                     if (Physics.Raycast(CameraMain.transform.position, CameraMain.transform.forward, RayCastLength, RayMask.value)) {
                         CarryableObject.transform.parent = Character.transform;
                         CarryableObject.transform.position = new Vector3(Character.transform.position.x + 1, Character.transform.position.y + 1, Character.transform.position.z + 1);
                         CarryableObject.transform.rotation = Character.transform.rotation;
                         CarryableObject.GetComponent<Rigidbody>().useGravity = false;
                         ItemPickedUp = true;
                         if(Handle != null){
                             Handle.GetComponent<Collider>().enabled = false;
                         }
                         if(HandleExtra != null){
                             HandleExtra.GetComponent<Collider>().enabled = false;
                         }
                         if(BladeHolder != null){
                             BladeHolder.GetComponent<Collider>().enabled = false;
                         }
                         if(Blade != null){
                             Blade.GetComponent<Collider>().enabled = false;
                         }
                     }
                 }
             }
          
             void ItemDrop() {
                 if(ItemPickedUp == true){
                     CarryableObject.transform.parent = null;
                     CarryableObject.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + OffsetDropPosition);
                     CarryableObject.transform.rotation = Character.transform.rotation;
                     CarryableObject.GetComponent<Rigidbody>().useGravity = true;
                     ItemPickedUp = false;
                     if(Handle != null){
                         Handle.GetComponent<Collider>().enabled = true;
                     }
                     if(HandleExtra != null){
                         HandleExtra.GetComponent<Collider>().enabled = true;
                     }
                     if(BladeHolder != null){
                         BladeHolder.GetComponent<Collider>().enabled = true;
                     }
                     if(Blade != null){
                         Blade.GetComponent<Collider>().enabled = true;
                     }
                 } 
             }
         }
     
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bodrp · May 14, 2017 at 12:06 AM 0
Share

When is ItemPickUp() called and when is CarryableObject assigned?

Any reason why CarryableObject is not passed as a parameter?

You should update your question accordingly.

avatar image RobAnthem · May 14, 2017 at 06:30 PM 1
Share

Something about this doesn't seem right.

Why does your pickup button not trigger a raycast, then each item be a seperate object?

There is way too much going on here for a simple Raycast pickup script.

avatar image RVDL_IT RobAnthem · May 14, 2017 at 06:36 PM 0
Share

Would you please be so kind as to elaborate a little bit more on how I do the seperate objects thing?

avatar image RobAnthem RVDL_IT · May 14, 2017 at 07:47 PM 0
Share

I don't know what exactly you are ai$$anonymous$$g for, but I feel like this is more along the lines of what you want.

 using System;
 using System.Collections.Generic;
 using System.Text;
 using UnityEngine;
 
 namespace myGame
 {
     public class Inventory : $$anonymous$$onoBehaviour
     {
         public GameObject CarryableObjectSlot;
         public GameObject CarriedItem;
         public $$anonymous$$eyCode Pickup$$anonymous$$ey;
         public float PickupRange;
         public float TossRange;
         void Update()
         {
             if (Input.Get$$anonymous$$ey(Pickup$$anonymous$$ey))
             {
                 if (!CarriedItem)
                 {
                     PickupItem();
                 }
                 else
                 {
                     DropItem();
                 }
             }
         }
         public void PickupItem()
         {
             Ray ray = new Ray(Camera.current.transform.position, Camera.current.transform.forward);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, PickupRange)){
 
                 if (hit.collider.tag == "CarryableObject")
                 {
                     CarriedItem = hit.collider.gameObject;
                     CarriedItem.transform.position = CarryableObjectSlot.transform.position;
                     CarriedItem.transform.rotation = CarryableObjectSlot.transform.rotation;
                     CarriedItem.transform.SetParent(CarryableObjectSlot.transform);
                     CarriedItem.GetComponent<Rigidbody>().useGravity = false;
                 }
             }
         }
         public void DropItem()
         {
             CarriedItem.transform.position = transform.forward * TossRange;
             CarriedItem.transform.SetParent(null);
             CarriedItem.GetComponent<Rigidbody>().useGravity = true;
             CarriedItem = null;
         }
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by yaezah · May 14, 2017 at 06:13 PM

I think you might want to specify CarryableObject as that specific gameobject you're trying to pick up, instead of assigning it to every item on the scene (that's why your'e picking up all the items at once).

Also have you tried changing from HandleExtra.GetComponent().enabled = false; to HandleExtra.collider.enabled = false;

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RVDL_IT · May 14, 2017 at 07:02 PM 0
Share

Would you please be so kind as to elaborate a little bit more on how I do the first thing that you said?

avatar image yaezah RVDL_IT · May 14, 2017 at 07:35 PM 1
Share
 if (Physics.Raycast(Camera$$anonymous$$ain.transform.position, Camera$$anonymous$$ain.transform.forward, RayCastLength, Ray$$anonymous$$ask.value)) {
                          CarryableObject = this.gameObject;
                          CarryableObject.transform.parent = Character.transform;


I would try that above, then when you drop it make it null

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

318 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bugs in a pick up items script. 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

For Some reason, collider check has extra collider. 2 Answers

Make sure levels assets are loaded before switching? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges