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 podusov.maxim · Jul 27, 2014 at 07:22 AM · cameracentersight

How take item if I am bringing rear sight and press key E?

I want take item if I am bringing rear sight and press key E.

I add trigger for Item

//item.cs

 public void OnTriggerStay(Collider other){
   if (other.tag == "Player"){
     Inventory.GetInventory().TakeItem();   
   }
 }

//inventory.cs

 public void TakeItem(){
         RaycastHit hit;
         Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
 
         if (Input.GetKeyDown (KeyCode.E)) {
             if (Physics.Raycast(ray, out hit)){
                     Item item = hit.transform.GetComponent<Item>();
                     if (item != null){
                         item.audioTakeItem.Play();
                         items.Add(item);
                         item.gameObject.SetActive(false);
                     }
             }
         }
     }


But this code wrong. I can take item if rear sight in not directed item.

Sorry my wrong English =)

alt text

ask.png (179.7 kB)
Comment
Add comment · Show 9
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 ShadoX · Jul 27, 2014 at 07:28 AM 0
Share

perhaps http://docs.unity3d.com/ScriptReference/Physics.CapsuleCast.html ?

avatar image rutter · Jul 27, 2014 at 07:42 AM 0
Share

What's the problem? Are you failing to pick up the item? Are you picking up an item that you shouldn't be able to? When does it break, and how is it breaking?

avatar image podusov.maxim · Jul 27, 2014 at 08:02 AM 0
Share

I can take this item, but I dont take, becouse rear sight not directed this item. alt text

ask2.png (187.6 kB)
avatar image Hullu · Jul 27, 2014 at 08:03 AM 0
Share

Are you sure the crosshair is in the middle?

avatar image podusov.maxim · Jul 27, 2014 at 08:19 AM 0
Share

Yes, If I understand correctly.

Yes, If I understand correctly.

ask3.png (20.5 kB)
Show more comments

1 Reply

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

Answer by incorrect · Jul 27, 2014 at 09:36 AM

So I've made it like this and it works just as expected.

 using UnityEngine;
 using System.Collections;
 
 public class RayCaster : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown (KeyCode.E)) {
             Debug.Log ("Pressed E");
             Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 if(hit.distance < 1f)    hit.collider.gameObject.SendMessage("Destroy");
             }
         }
     }
 }


Figure out by yourself how to make the rest of logic.

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 podusov.maxim · Jul 27, 2014 at 11:38 AM 0
Share

Thank you. If I check clickable item or not, I use this code:

 Item item = hit.transform.GetComponent<Item>();               
 if (item != null){....}

Or this is bad practice?

avatar image podusov.maxim · Jul 28, 2014 at 05:20 AM 0
Share

All right) Also I used this small hack: alt text

ask4.png (225.5 kB)
avatar image incorrect · Jul 28, 2014 at 09:19 AM 0
Share

Yeah, as I know you're doing it right. You can check if the hited object has component or can make it a much more universal way.

      void Update () {
             if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E)) {
                 Debug.Log ("Pressed E");
                 Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
                 RaycastHit hit;
                 if (Physics.Raycast(ray, out hit))
                 {
                     if(hit.distance < 1f)   hit.collider.gameObject.Send$$anonymous$$essage("Interact", Send$$anonymous$$essageOptions.DontRequireReceiver);
                 }
             }
         }

So if it is pickable object, it's Interract() method will add something to inventory and destroy it's instance in the world, if it is door it will open, but the interaction method will be same for everything.

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

7 People are following this question.

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

Related Questions

How to make camera position relative to a specific target. 1 Answer

How to Keep Player in the Center of the Screen? 0 Answers

Gui.drawtexture using center instead of corner for positioning 1 Answer

Center an object to a camera 2 Answers

FPS collider center offsets camera pivot? 0 Answers


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