Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ThatMartinFellow · Dec 01, 2014 at 01:52 AM · c#key

Press 'e' key on object? (C#)

Hey all! I have it set up so pressing the e key works, but the problem is, it works anywhere. I tried to have it on a mouse enter event, but for some reason that doesn't work. I'm basic level at C#, currently in a 202 class at my university, if that helps you understand my knowledge base.

Any and all help appreciated! :)

Here's the code I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class scrNPC01 : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyUp (KeyCode.E)) {
             Debug.Log (gameObject.name);
             
         }
     }
 
         
 }
 

The debug.log is just for testing, but the principle is similar. For using the e key for interacting with objects/npcs.

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

Answer by MuffinMyst · Dec 01, 2014 at 02:16 AM

umm... there are several ways you can do this. (and I suppose you could even use a mouse enter event) The way I've done similar to what I think you want to do.

You have to put the "OnMouseEnter" into a script that you attach to what ever object that you want to see the name for. For example:

 OnMouseEnter(){
   Debug.log(gameObject.name);
 }

but... if you want avoid attaching this onmouse enter script for every object in your game. You could also try RayCasting.

I think there is a way to Cast a "ray" from your mouse, that will allow you to access information from the object hit with that "ray" You would have this "ray casting" script on your player

You could have it when you click the mouse, or when you mouse over and object and hit a key... (such as e)

RaycastHit

This isn't a full working script but might give you some starting ideas, and the link should assist with understanding

 Ray ray;
 RaycastHit hit;
 float distance; // distance you want it to work
 
 
  if (Input.GetButtonUp( KeyCode.E ) ){
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      if(Physics.Raycast(ray, hit, distance)){
        Debug.log(hit.name);
            
    }
       
  }


I'm not fluent with Rays yet (I have about as much classroom experience as you do, and about a month of fiddling with scripts people provide here.

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
avatar image
0

Answer by jtagamesinfo · Apr 23, 2021 at 07:59 AM

We created a tutorial with a complete working interact system that you can modify or leave it how it is.
Hope this helps!

https://www.youtube.com/watch?v=lZThP8KG1W0

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Health Regen 1 Answer

press e to speek -1 Answers

Does Scripting Jump exist? (How To use/write goto) 3 Answers

My AI keeps flickering between stopping and moving. 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