Skip to content

Instantly share code, notes, and snippets.

@radiatoryang
Created November 13, 2020 18:52
Show Gist options
  • Save radiatoryang/f0e4f62421a9c5b751f6a49c19a967a0 to your computer and use it in GitHub Desktop.
Save radiatoryang/f0e4f62421a9c5b751f6a49c19a967a0 to your computer and use it in GitHub Desktop.
Unity C# input code demos for Studio 1, Fall 2020 at NYU Game Center
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AxisInput : MonoBehaviour
{
void Update()
{
// Input Axis is a virtual joystick... unlike GetKey, it returns -1.0 - +1.0
// e.g. holding your joystick to the left: -1.0f
// holding you joystick to the right: +1.0f
// not touching joystick at all: 0f
// WASD controller?
// float horizontal = Input.GetAxis("Horizontal"); // A+D capital H!!!
// float vertical = Input.GetAxis("Vertical"); // W+S
// mouse controller?
float horizontal = Input.GetAxis("Mouse X") * 0.5f; // MOUSE_X
float vertical = Input.GetAxis("Mouse Y") * 10f; // vertical mouse speed, 0 when not moving
transform.Translate(
horizontal * Time.deltaTime * 5f,
vertical * Time.deltaTime * 5f,
0f
);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CursorInput : MonoBehaviour
{
void Update()
{
// 1. get mouse cursor position
Vector3 mouseCursor = Input.mousePosition; // in pixel screen-space
// 2. convert mouse cursor position into WORLD SPACE / game space
Vector3 worldPosition = Camera.main.ScreenToWorldPoint( mouseCursor );
worldPosition.z = 0f;
// 3. move triangle to my world position
transform.position = worldPosition;
// bonus: hide the mouse cursor?
// IMPORTANT: hide / lock cursor only when the user clicks, to ensure this window has focus
if ( Input.GetMouseButtonDown(0)) { // did user left-click? 1 = right click, 2=middle click
// clicking = give focus to this application window
Cursor.visible = false; // hide cursor?
// Cursor.lockState = CursorLockMode.Locked; // good for mouse look?
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KeyInput : MonoBehaviour
{
float timeHeldDown = 0f; // timer that tracks how long we held down the button?
void Update()
{
// KEY PRESS INPUTS
// GetKeyDown happens on the first frame when you start pressing down a key
if ( Input.GetKeyDown(KeyCode.Space) ) {
transform.Rotate( 0f, 0f, 5f );
}
// GetKeyUp happens on the first frame when you RELEASE a key
if ( Input.GetKeyUp(KeyCode.Space) ) {
transform.Rotate( 0f, 0f, -5f );
}
// KEY HOLDING DOWN
// GetKey happens EVERY FRAME as long as you're holding down the key
if ( Input.GetKey(KeyCode.Space ) ) {
transform.localScale += Vector3.one * Time.deltaTime; // grow bigger
}
// TIMED PRESS? COMBINES ALL THREE
// if we're holding down G on keyboard, then we turn red gradually / charge up?
if ( Input.GetKey(KeyCode.G) ) {
if ( timeHeldDown < 2f ) {
timeHeldDown += Time.deltaTime; // deltaTime = duration of frame in seconds
// e.g. if game is running at 60 FPS, dT = 1/60... 10 FPS? dT = 1/10
Debug.Log( timeHeldDown );
}
// hacky way of turning red, don't do this
GetComponent<SpriteRenderer>().color = Color.Lerp(
GetComponent<SpriteRenderer>().color,
Color.red,
Time.deltaTime * 2f
);
}
// when you release G, you reset
if ( Input.GetKeyUp(KeyCode.G ) ) {
timeHeldDown = 0f; // reset
GetComponent<SpriteRenderer>().color = Color.white;
// expel energy, etc. do game code here
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment