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 Exagerate · Feb 27, 2013 at 03:09 AM · c#javascriptgetkeydownonkeydown

Calling GetKeyDown for C# instead of Javascript

Hi there guys,

I've used the following code in Javascript for using the numeric 2 button to call a trigger in my scene and i want to know what the equivalent way to write it would be for C#

If(Input.GetKeyDown(KeyCode.Alpha2)){

I want to have it here instead of being on mouse down, on the same call as above- on 2 being pressed on the keyboard.

Void OnMouseDown ()

Is there a library anywhere which has this kind of thing listed?

Comment
Add comment · Show 1
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 Exagerate · Feb 27, 2013 at 03:12 AM 0
Share

And this is the context of my script. I want to make it happen on "2" being pressed, not On$$anonymous$$ouseDown!

 using UnityEngine;
 using System.Collections;
 
     public class passdoorfront : $$anonymous$$onoBehaviour
 {
     
     bool b = true;
 
     private Vector3 v3To;
 
     void On$$anonymous$$ouseDown ()
         
     {
        SetOpenClose (b);
        b = !b;
     }
     
     void SetOpenClose(bool bOpen) {
        if (bOpen) {
        iTween.$$anonymous$$oveBy(gameObject, iTween.Hash("z", 0.7, "easeType", "easeInOutExpo", "delay", 1, "Speed", 0.2));
         }
        else {
        iTween.$$anonymous$$oveBy(gameObject, iTween.Hash("z", -0.7, "easeType", "easeInOutExpo", "delay", 1, "Speed", 0.2));
         }
     }
 }

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by liamcary · Feb 27, 2013 at 06:07 AM

You're pretty much there. You can check for key down in an update function. e.g:

 void Update()
 {
    if(Input.GetKeyDown(KeyCode.Alpha2))
    {
       // do stuff
    }
 }
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 Eric5h5 · Feb 27, 2013 at 03:40 AM

You already wrote what the equivalent is in C#. (Minus the inappropriate capitalization, of course.) It's the same.

Comment
Add comment · Show 4 · 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 Exagerate · Feb 27, 2013 at 03:50 AM 0
Share

Hi Eric,

Thanks for your comment- The problem I have is that I don't understand how to structure it within the Void argument, would it be this, for instance?:

Void Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha2) ()

avatar image Eric5h5 · Feb 27, 2013 at 04:00 AM 0
Share

No, void is what the function returns. Are you sure you want to use C#? If you're familiar with Unityscript you should just continue to use that. Otherwise I'd recommend doing some C# tutorials so you can learn the language, since that's not really Unity-related per se.

avatar image Exagerate · Feb 27, 2013 at 04:21 AM 0
Share

Yes, but all my iTween scripts use C#, and it's a job to know what exactly to search in the ol' search engine, but believe me- i've been trying it for a while.

avatar image AlucardJay · Feb 27, 2013 at 05:25 AM 0
Share

Here's some links I found useful in converting between C# and JS :

  • http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

  • http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

avatar image
0

Answer by Exagerate · Feb 27, 2013 at 06:08 AM

I managed to figure out what I was doing at long last! I stuck to rearranging the code and eventually found a helpful topic, this is a code to open doors with this script attached to them at the press of a keyboard key! In this example, key "2" will open it:

 using UnityEngine;
 using System.Collections;
 
     public class passdoorfront : MonoBehaviour
 {
     
     bool b = true;
 
     private Vector3 v3To;
 
     void Update () {
         
     if(Input.GetKeyDown(KeyCode.Alpha2))
         
     {
        SetOpenClose (b);
        b = !b;
     }
     }
     
     void SetOpenClose(bool bOpen) {
         if (bOpen) {
        iTween.MoveBy(gameObject, iTween.Hash("z", 0.7, "easeType", "easeInOutExpo", "delay", 1, "Speed", 0.2));
         }
        else {
        iTween.MoveBy(gameObject, iTween.Hash("z", -0.7, "easeType", "easeInOutExpo", "delay", 1, "Speed", 0.2));
         }
     }
 }
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

12 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Problem Javascript to C# 3 Answers

dealing with gameObject formations 1 Answer

how do i make teams for a rts game 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