Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by killfast27 · Oct 17, 2014 at 07:02 PM · javascriptpickupcrafting

Simple crafting system ; item pickup

Hello i have two scripts that are supposed to work in tandem One is called manageInv.JS its supposed to track whether or not you have collected a log or a plank and if you do have a log or plank you can make multiple things from those. I have another script that i just named Raycast.JS which is supposed to cast a raycast line at objects and distinguish their tags, and if they have either of the tags it will display a text saying "pickup" and let you pick them up by pressing "E". It also destroys the object then tells the manageInv script that you have collected them. The problem that I am encountering is that when I look at the two object that have those two tags (Log and Plank) it doesn't display the text and doesn't let me pick them up and doesn't tell the manageInv script that the player has collected the objects. Here is my code.

 manageInv.JS
 
 #pragma strict
 
 var logCollected : boolean = false;
 var plankCollected : boolean = false;
 
 private var showPlankGUI : boolean = false;
 private var showWallGUI : boolean = false;
 
 var Plank : Transform;
 var Wall : Transform;
 var player : Transform;
 var Axe : Transform;
 
 function Update()
 {
     if(plankCollected == true)
     {
         showWallGUI = true;
     }
     
     if(plankCollected == true && Input.GetKey("l"))
     {
         MakeWall();
     }
     
     if(logCollected == true)
     {
         showPlankGUI = true;
     }
     
     if(logCollected == true && Input.GetKey("p"))
     {
         MakePlank();
     }
     
     if(logCollected == true && plankCollected == true && Input.GetKey("o"))
     {
         MakeAxe();
     }
 }
 
 function onGUI()
 {
     if(showWallGUI == true)
     {
         GUI.Box(new Rect (200, 200, 200, 25), "Press P to make a wall segment");
     }
     
     if(showPlankGUI == true)
     {
         GUI.Box(new Rect (200, 200, 200, 25), "Press P to make planks");
     }
 }
 
 function MakePlank()
 {
     Instantiate (Plank, player.transform.position, Quaternion.identity);
     logCollected = false;
     showPlankGUI = false;
 }
 
 function MakeWall()
 {
     Instantiate (Wall, player.transform.position, Quaternion.identity);
     plankCollected = false;
     showWallGUI = false;
 }
 
 function MakeAxe()
 {
     Instantiate (Axe, player.transform.position, Quaternion.identity);
     plankCollected = false;
 }







 Raycast.JS
 
 #pragma strict
 
 var Log : GameObject;
 var Plank : GameObject;
 
 private var canHover : boolean = false;
 
 function Update()
 {
     var fwd = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     
     if(Physics.Raycast(transform.position, fwd, hit))
     {
         if(hit.distance <= 5.0 && hit.collider.gameObject.tag == "Log")
         {
             canHover = true;
             
             if(Input.GetKeyDown("e"));
             {
                 Destroy(Log);
                 var ManageScript : ManageInv = GameObject.Find("First Person Controller").GetComponent(ManageInv);
                 ManageScript.logCollected = true;
                 
             }
         }
             
         else
         {
             canHover = false;
                 
         }
         
         if(hit.distance <= 5.0 && hit.collider.gameObject.tag == "Plank")
         {
             canHover = true;
             
             if(Input.GetKeyDown("e"));
             {
                 Destroy(Plank);
                 ManageScript.plankCollected = true;
                 
             }
         }
             
         else
         {
             canHover = false;
                 
         }
         
         
     }
 }
 
 function OnGUI()
 {
     if(canHover == true)
     {
         GUI.Box(Rect(Screen.width / 2 - 10, Screen.height / 1 - 100, 150, 20), "Pick up");
     }
 }
 



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

1 Reply

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

Answer by Habitablaba · Oct 17, 2014 at 07:31 PM

Casting a ray straight forward from your character at a distance of 5 will only find things that are at least as tall as the character's position. If your character's transform is located at the center, you'll miss short things for example.
You'll also need the object to be directly in front of you, so if it is slightly to the left or right, you may miss it.

Check out sphere cast, instead, and see if that doesn't work better for you

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Weapon pickup Script...HELP!(Java) 0 Answers

Weapon pick up and switching script 2 Answers

Item pickup and activating an item afterwards help 2 Answers

Problem assigning transform variable to prefab. 1 Answer

How to differentiate between prefabs on collision. 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