• 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
1
Question by drjenkin · Apr 09, 2016 at 06:20 AM · gameobjectscommunication

Best approach to communicating between player and interactive objects (doors, lightswitches, switches)

I have a simple premise: that typical 'press 'e' to use' function. You use it to open doors, turn on light switches and interact with things in the world.

I wanted to know if anyone had found a solid approach to this. Right now I have my player with a FPS camera and an object I want to interact with (pressing e starts an animation on the object). The script and code for this is attached to the object. The setup I have has the object checking the proximity of the player (vector3.distance) and activating the animation if the user presses 'e' when the distance is less than x.

The object uses a public Gameobject variable to gain access to the vector3 data of the player (i drag the player gameobject into the slot on the object I interact with).

This means that for every object i want to make interactive will need to have a script and a reference to the player gameobject.

Is there a more efficient means of communication? I have read that "sendmessage" is not ideal (and im also not 100 per cent clear on the implementation)

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 vyonox · Apr 09, 2016 at 07:19 AM

I prefer to do the opposite. Add an script to the player that detects when the user press E. Then looks for objects in the proximity of the player and checks if the object is "interactable", for example with a tag, a layer or just trying to get a component from the object.

Attach the same script to all the interactable objects and get that script from the player script with GetComponent< InteractionScript>(). In the script, add a public Interact() function that starts the animation or whatever.

I see the problem that every different kind of object can have a different interaction result. Then, the Interact() function will need to deal with this.

I don't know if there is a better approach. I just don't like the idea of objects checking if the user has pressed a key.

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 drjenkin · Apr 09, 2016 at 12:12 PM 0
Share

Yeah I get you. I've updated it so now but i'm not sure if it's a more streamlined solution.

Now I'm raycasting from the player, using the ray's hit.collider.gameobject.Getcomponent to access a simple bool script on the object which communicates with a specific script attached to the same interactive object and runs a function to open, unlock, turn on / whatever when it detects the bool in the common script changes to true.

So though the individual objects arent checking if the player has hit e, they are checking if the common isTouched bool changes from false to true.

avatar image drjenkin drjenkin · Apr 10, 2016 at 10:19 AM 0
Share

I have solved this problem as well in a fantastic way after going back to the C# book. Cheers again!

avatar image filibis drjenkin · Jul 10, 2017 at 02:19 PM 0
Share

Hey @drjenkin , i'm dealing with similar approaches/issues. Could you please explain your 'fantastic way' with some possible examples/workflows?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Passing references between game objects 1 Answer

Making an endless hallway with random objects spawning 0 Answers

How to make the edges of an invisable game object, visible 0 Answers

Weather Timer (Beginner to scripting) (In multiplayer) 0 Answers

How to get the tags of objects if they aren't in the scene? 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