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 Kalbytron · Dec 05, 2015 at 03:54 AM · script.universalactioninteractive

How to make an universal action button script

Press E to open a door, turn on a radio, pick up a thing, talk to that guy... The button that does it all. My problem is writing the script. I though about using delegates, events, inheritance, or abstracts in the Interact script so it can work with any named script that has the universal function in order to interact. If this universal function is called, then it executes whatever is inside, like the OnTriggersXXX, OnCollisionXXX, OnMouseClick... But I don't have the know-how to do this. Is this right way to go? Is there something better?

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
2

Answer by feliperw · Apr 19, 2016 at 07:05 PM

If it's a First Person Camera, you can use Raycast when the action button is pressed and call a script that activates a UnityEvent that holds the action for that object. If it is 3rd Person, same principle but instead of Raycast use Triggers. OnTriggerEnter set the interactable object in the player script and unset OnTriggerExit. Call the interactable object when the action key is pressed.

The Interactable Objects will use a script like this:

 using UnityEngine;
 using UnityEngine.Events;
  
 public class ActionObject: MonoBehaviour {
      public UnityEvent myUnityEvent;
  
      void Awake() {
          if (myUnityEvent == null)
              myUnityEvent = new UnityEvent();
      }
  
      public void DoAction() {
          myUnityEvent.Invoke();
      }
  
  }

First Person Player

 using UnityEngine;
  
 public class FirstPersonPlayer: MonoBehaviour {
      public Camera camera;

      void Update() {
          if(Input.GetKeyDown("E"))
          {
              //Raycast
              RaycastHit hit;
              Ray ray = camera.ScreenPointToRay(Input.mousePosition);
     
              if (Physics.Raycast(ray, out hit)) 
              {
                   GameObject objectHit = hit.transform.gameObject;

                   //Try getting ActionObject Component from the retrieved object
                   ActionObject actionObj = objectHit.getComponent<ActionObject>();

                   if(actionObj!=null)
                   {
                        //if ActionObject is found call DoAction()
                        actionObj.DoAction();
                   }
               }

          }
      }
  
  }

3rd Person Player

 using UnityEngine;
  
 public class ThirdPersonPlayer: MonoBehaviour {
      public ActionObject actionObj;

      void Update() {
          if(Input.GetKeyDown("E"))
          {
              if(actionObj!=null)
              {
                   actionObj.DoAction();
              }
          }
      }

      void OnTriggerEnter(Collider other) {
               //Try getting and setting ActionObject Component
               actionObj = other.gameObject.getComponent<ActionObject>();
      }

     void OnTriggerExit(Collider other) {
               //unset the actionObject
               actionObj = null;
      }

  }

You can work from here and customize those scripts to fit your needs.

I made a package with the scripts and samples. It can be downloaded in my website http://www.fenika.net/?p=7

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 Albathroz · Oct 02, 2016 at 06:47 PM 0
Share

$$anonymous$$an, you da real $$anonymous$$VP!!! Your project has exactly what I needed to make my RPG game. Thank you so much.

avatar image feliperw · Oct 03, 2016 at 03:11 PM 0
Share

You are welcome @Albathror. Glad I could help ;)

avatar image aayoub_unity · Feb 18, 2018 at 05:11 AM 0
Share

Thank you Felipe for your help

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

36 People are following this question.

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

Related Questions

Action (by collider) 0 Answers

Show device camera preview (Android & iOS) 1 Answer

How can i create/spawn some objects and set random angle for each one ? 0 Answers

I'm getting some errors in the script how can i fix them ? 1 Answer

How to procedurally make a crisp object look smooth and curved. 2 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