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 /
  • Help Room /
avatar image
0
Question by christianjulch · Apr 19, 2016 at 05:58 AM · unity 5scripting problemraycastingpickup

Raycast not sensing object on ground? cant pick up, Help Please !!! :)

Problem: As part of an inventory script im working on i found something that has had me stuck for days and that is that the ray cast that senses the object is not doing so, when the object is sensed through an id check it is supposed to display a pick up text. The problem is that i have drawn the ray out and it appears 100% to be going strait through the collider but isn't sensing it. Now when i step back a few units it then works. I have all the colliders set up perfectly but im not sure with the code. I would appreciate any help, thanks ahead of time.

Please Note: The two scripts below communicate with each other, the item pick up script checks the id and the bool pickupable.

ItemPickUp Script:

 public Inventory Inv;
 public GameObject pickUpUI;
 bool addedItem;

 void Start ()
 {
     Inv = GameObject.Find ("Inventory Panel").GetComponent<Inventory> ();
     pickUpUI.SetActive (false);
 }
 

 void Update () 
 {
     Debug.DrawRay (Camera.main.transform.position, Camera.main.transform.forward * 3, Color.green);

     RaycastHit hit;
     if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 3, -1, QueryTriggerInteraction.Collide))
     {
         bool pickup = hit.transform.gameObject.GetComponent<Item3D> ().pickUpable;

         if (pickup == true) {
             Debug.Log ("Working 1");
             int itemID = hit.transform.gameObject.GetComponent<Item3D> ().id;
             pickUpUI.SetActive (true);

             if (Input.GetKeyDown (KeyCode.E)) {
                 Debug.Log ("Working 2");
                 Inv.AddItem (itemID);
                 Destroy (hit.transform.gameObject);


                  
             }
         } else {
             pickUpUI.SetActive (false);
         }

     }
 }


} Item3D Script:

 public bool pickUpable = false;
 public int id;

 Inventory inv;

 void Start()
 {
     //inv.items [id].itemID = id;
 

 }

}

Comment
Add comment · Show 6
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 RiQQ92 · Apr 19, 2016 at 06:04 AM 0
Share
          int pickup = hit.transform.gameObject.GetComponent<Item3D> ().pickUpable;
          if (pickup == true) {

As first guess i think the problem lies there. You are assigning bool value to int and then testing the int value against a boolean true value, which i think should give error already.

avatar image christianjulch · Apr 19, 2016 at 01:12 PM 0
Share

Yeah sorry that was a quick add to the code outside of unity i forgot to change the int to a bool.

@RiQQ92

avatar image RiQQ92 · Apr 19, 2016 at 01:52 PM 0
Share

Okay then the code seems pretty good to me, maybe it would be good idea to check first if the hit.transform is actually an item that contains Item3D script?

Then its bit hard to guess whats going on, but what i have encountered something similar before was like i had my ray starting at middle of my character and it collided with the characters collider unless i was walking backwards. So it would be a good idea to check where the ray is colliding with hit.transform.name when it should be colliding with the pickup.

@christianjulch

avatar image christianjulch RiQQ92 · Apr 19, 2016 at 02:57 PM 0
Share

I will check out the hit.transform.name thanks

avatar image christianjulch RiQQ92 · Apr 20, 2016 at 02:53 AM 0
Share

I checked it out and the player transform seems to be blocking it do you know how to allow the raycast through. @RiQQ92

avatar image RiQQ92 · Apr 20, 2016 at 11:36 AM 0
Share

You could change the rays starting position like

 Camera.main.transform.position + Camera.main.transform.forward

but thats rather dirty way, so best way in my opinion would be to change the rays collision layers and give some specific layer to player like 'IgnoreRaycast'

Easy way to get correct layers for your ray is to set them in inspector, and for this you need to set:

 public Layer$$anonymous$$ask myRayCollisions;

and then just in your code you can write it there like:

      if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 3, myRayCollisions.value, QueryTriggerInteraction.Collide))
      {

Other way to set your collision layer is bit shifting but i wont go into that. Hope you get it working now, cheers.

@christianjulch

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I make this script actually load the level 1 Answer

FormatException: Input string was not in the correct format 0 Answers

Jittery 3D Turning 1 Answer

Quality changing not working 1 Answer

Script isn't working. Easy solutions to adding dialogue? Help ASAP - project due in a few days 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