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 DavidDebnar · Jun 27, 2011 at 04:18 PM · rigidbodyraycastdistancepickupvisible

Picking up rigidbody objects

Hey there again! SO here is the deal! I made a script that i can attach to a rigidbody/object, and than, when the player comes to it, is looking at it, isnt hodling any other object and there is nothing in the way can pick up. It works perfectly. BUT I need to make something else. I need to make a code, thats on the player and checks if there is a rigidbody around, and if there is nothing between etc. So the same thing, just casted from the player. Reason why I wanna do it is, because I am checking all that things after player E key is pressed, but I need to make it check it realtime, because i wanna make a GUI text like Press E for pickup, so when there are 50 objects plus some other things, FPS and Draw calls will go crazy.

Here is the original code:

Code

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by SilverTabby · Jun 27, 2011 at 05:14 PM

Normally I wouldn't write code from scratch for a question, but you got lucky because I'm feeling nice today.

Do something like this on a script somewhere on your player


var fromWhere : transform;

var dist: float = 2;

var pickUpLayerMask : LayerMask;

function LateUpdate()

{

 var hit : RaycastHit;
 
 var fwd: Vector3 = fromWhere.TransformDirection(Vector3.forward);
 
 if(  Physics.Raycast(fromWhere.position, fwd, hit, dist, pickUpLayerMask)  )
 
    if(hit.rigidbody)
 
        if(hit.rigidbody.tag == "Insert tag that says I can be picked up here")
 
        {
 
           /*we have found a rigidbody that can be picked up in the center
           of the screen that is close enough to work with. do stuff */
 
        }
 

}


I normally only help with small things like bug fixes and answering questions, not building features from scratch like I did here, so keep that in mind for any further questions you ask here.

Also, I couldn't access the code you put in the link in the question. Please try a different code pasting method next time.

Comment
Add comment · Show 3 · 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 DavidDebnar · Jun 27, 2011 at 06:04 PM 0
Share

cool thanks! i already made somethink like this, but this is looking better to work with :-)...cant access the code? its right here!

https://docs.google.com/document/d/1RHTx7JSl7XN_L$$anonymous$$k0xwScT35YV6PoHDsAYOW6vEIpQ34/edit?hl=en_US

avatar image DavidDebnar · Jun 27, 2011 at 06:11 PM 0
Share
  • you had there a little mistake, when declaring a var there is a upcase T in transform ;-)

avatar image DavidDebnar · Jun 27, 2011 at 06:55 PM 0
Share

okayyyy....THAN$$anonymous$$S BUD! Its working and it will be on the web soon, so BIG THAN$$anonymous$$S :-)

avatar image
0

Answer by Schockerboy · Apr 13, 2016 at 03:52 PM

All Credits to @SilverTabby. Thanks man! I needed it in C#, so I translated it and now want to share it.

 using UnityEngine;
 using System.Collections;
 
 public class PickUpScript : MonoBehaviour
 {
 
     Transform fromWhere;
 
 
     float dist = 2;
 
 
     LayerMask pickUpLayerMask;
 
     void LateUpdate()
     {
 
 
         RaycastHit hit;
 
         Vector3 fwd = fromWhere.TransformDirection(Vector3.forward);
 
         if (Physics.Raycast(fromWhere.position, fwd, out hit, dist, pickUpLayerMask))
         { 
             if (hit.rigidbody)
             { 
                 if (hit.rigidbody.tag == "Insert tag that says I can be picked up here")
 
                 {
 
                     /*we have found a rigidbody that can be picked up in the center
                     of the screen that is close enough to work with. do stuff */
 
                 }
             }
         }
     }
 }



Comment
Add comment · 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

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

Calculate where an object is going to land 0 Answers

Restrict two objects from getting too close. 1 Answer

How to get the distance between two objects in feet/meter? 1 Answer

Is object at least partly visible? 1 Answer

Enemy AI help with height check 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