- Home /
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?
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));
}
}
}
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
}
}
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.
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) ()
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.
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.
Here's some links I found useful in converting between C# and JS :
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));
}
}
}
Your answer
Follow this Question
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