Unity3d Input

Embed Size (px)

Citation preview

  • 8/10/2019 Unity3d Input

    1/119

    Unity3d

    ()

    2013/12/28

  • 8/10/2019 Unity3d Input

    2/119

    Mouse()

  • 8/10/2019 Unity3d Input

    3/119

    OnMouse(Down, Drag, Enter, Exit, Over, Up)

    (GameObject)

  • 8/10/2019 Unity3d Input

    4/119

    OnMouseDown

    // Loads the level named "SomeLevel" as a

    response

    // to the user clicking on the object

    function OnMouseDown () {Application.LoadLevel ("SomeLevel");

    }

  • 8/10/2019 Unity3d Input

    5/119

    OnMouseDrag

    // Darken the material color while user holds

    down the mouse.

    function OnMouseDrag () {renderer.material.color -= Color.white * Time.deltaTime;

    }

  • 8/10/2019 Unity3d Input

    6/119

    OnMouseEnter

    // Attach this script to a mesh to make

    // it red when the mouse is over the mesh

    function OnMouseEnter () {

    renderer.material.color = Color.red;}

  • 8/10/2019 Unity3d Input

    7/119

    OnMouseExit

    // Assigns a white color to the material

    // attached to this mesh.

    function OnMouseExit () {

    renderer.material.color = Color.white;}

  • 8/10/2019 Unity3d Input

    8/119

    OnMouseOver// Fades the red component of the material to zero

    // while the mouse is over the mesh

    function OnMouseOver () {

    renderer.material.color -= Color(0.1, 0, 0) * Time.deltaTime;

    }

  • 8/10/2019 Unity3d Input

    9/119

  • 8/10/2019 Unity3d Input

    10/119

    Hierarchy Create Sphere

  • 8/10/2019 Unity3d Input

    11/119

    OnMouse(Enter, Over, Exit, Down)

    // Attach this script to a mesh to make

    // it red when the mouse is over the mesh

    function OnMouseEnter () {

    renderer.material.color = Color.red;

    }

    function OnMouseOver () {

    renderer.material.color -= Color(0.1, 0, 0) * Time.deltaTime;

    }

    function OnMouseExit () {

    renderer.material.color = Color.white;

    }

    function OnMouseDown () {

    renderer.material.color = Color.green;

    }

  • 8/10/2019 Unity3d Input

    12/119

    Sphere OnMouse

  • 8/10/2019 Unity3d Input

    13/119

  • 8/10/2019 Unity3d Input

    14/119

    Cube OnMouseDrag

  • 8/10/2019 Unity3d Input

    15/119

    JavaScript VC#

  • 8/10/2019 Unity3d Input

    16/119

    Input

  • 8/10/2019 Unity3d Input

    17/119

    Input.GetMouse

  • 8/10/2019 Unity3d Input

    18/119

    Input.GetMouse(Button, ButtonDown, ButtonUp)

    ()

  • 8/10/2019 Unity3d Input

    19/119

    Input.GetMouseButton// Detects clicks from the mouse and prints a message

    // depending on the click detected.function Update() {

    if(Input.GetMouseButton(0))

    Debug.Log("Pressed left click.");if(Input.GetMouseButton(1))

    Debug.Log("Pressed right click.");

    if(Input.GetMouseButton(2))

    Debug.Log("Pressed middle click.");

    }

  • 8/10/2019 Unity3d Input

    20/119

    Input.GetMouseButtonDown// Detects clicks from the mouse and prints a message

    // depending on the click detected.function Update() {

    if(Input.GetMouseButtonDown(0))

    Debug.Log("Pressed left click.");if(Input.GetMouseButtonDown(1))

    Debug.Log("Pressed right click.");

    if(Input.GetMouseButtonDown(2))

    Debug.Log("Pressed middle click.");

    }

  • 8/10/2019 Unity3d Input

    21/119

    Input.GetMouseButtonUp// Detects clicks from the mouse and prints a message

    // depending on the click detected.function Update() {

    if(Input.GetMouseButtonUp(0))

    Debug.Log("Pressed left click.");if(Input.GetMouseButtonUp(1))

    Debug.Log("Pressed right click.");

    if(Input.GetMouseButtonUp(2))

    Debug.Log("Pressed middle click.");

    }

  • 8/10/2019 Unity3d Input

    22/119

    Hierarchy Create GUI Text

  • 8/10/2019 Unity3d Input

    23/119

    GetMouse.jsfunction Update() {

    if(Input.GetMouseButton(0)){guiText.text="Pressed left click.";

    }

    if(Input.GetMouseButton(1)){guiText.text="Pressed right click.";

    }

    if(Input.GetMouseButton(2)){

    guiText.text="Pressed middle click.";}

    }

  • 8/10/2019 Unity3d Input

    24/119

    GUI Text

    GetMouse

  • 8/10/2019 Unity3d Input

    25/119

    MousePosition() & RayCast

  • 8/10/2019 Unity3d Input

    26/119

    OnMouse.js

    Input.mousePositionvar particle : GameObject;function Update () {

    if (Input.GetButtonDown ("Fire1")) {// Construct a ray from the current mouse coordinates

    var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    var hit : RaycastHit;if (Physics.Raycast (ray,hit,100)) {

    // Create a particle if hit

    Instantiate (particle, hit.point, transform.rotation);

    }

    }

    }

  • 8/10/2019 Unity3d Input

    27/119

    Main Camera OnMouse

    Particle Flame

  • 8/10/2019 Unity3d Input

    28/119

    MouseSensor.jsvar target1: Transform;

    var target2: Transform;

    function Update () {

    if (Input.GetMouseButton(0)) {

    var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    var hit: RaycastHit;

    if (Physics.Raycast(ray, hit)) {

    if (hit.transform == target1) {

    print("Hit target 1");} else if (hit.transform == target2) {

    print("Hit target 2");

    }

    } else {

    print("Hit nothing");

    }

    }

    }

  • 8/10/2019 Unity3d Input

    29/119

    Main CameraMouseSensor

    Target1 Cube

    Target2 Sphere

  • 8/10/2019 Unity3d Input

    30/119

    MouseScrollWheel.js

    Input.GetAxis("Mouse ScrollWheel")var testString : String = "";

    function OnGUI ()

    {

    var wheelValue = Input.GetAxis("Mouse ScrollWheel");

    if (wheelValue != 0){

    testString = testString + " " + wheelValue;

    GUI.Label(Rect(0,0,320,480),testString);

    }

    transform.Translate(0, wheelValue*10,0);}

  • 8/10/2019 Unity3d Input

    31/119

    Sphere

    MouseScrollWheel

  • 8/10/2019 Unity3d Input

    32/119

    Main Camerea

    MouseXY

  • 8/10/2019 Unity3d Input

    33/119

    MouseXY.jsvar mouse : Texture2D;

    var mousePs = Vector2.zero;

    function Update () {

    mousePs = Input.mousePosition;}

    function OnGUI () {

    Screen.showCursor = false;

    GUI.DrawTexture(Rect(mousePs.x,Screen.height-mousePs.y,25,25),mouse);gameObject.Find("GUITextH").guiText.text ="Screen.height:" +Screen.height.ToString();

    gameObject.Find("GUITextY").guiText.text ="mousePs.y:" +mousePs.y.ToString();

    var diffH;

    diffH=Screen.height-mousePs.y;

    gameObject.Find("GUITextHY").guiText.text =diffH.ToString();

    GUI.DrawTexture(Rect(0,100,25,25),mouse);

    }

  • 8/10/2019 Unity3d Input

    34/119

    MouseBMP

  • 8/10/2019 Unity3d Input

    35/119

    Input.GetButton, GetButtonUp, GetButtonDown

  • 8/10/2019 Unity3d Input

    36/119

    Input.GetButton, GetButtonUp, GetButtonDown

  • 8/10/2019 Unity3d Input

    37/119

    GameObject

    Create Empty

  • 8/10/2019 Unity3d Input

    38/119

    GameObject

  • 8/10/2019 Unity3d Input

    39/119

    Javascript

  • 8/10/2019 Unity3d Input

    40/119

    InputPress.jsfunction Update () {

    if(Input.GetButtonUp("Jump")){

    Debug.Log("We Have Hit the Space Bar!");

    }}

  • 8/10/2019 Unity3d Input

    41/119

    GameObject

    InputPress

  • 8/10/2019 Unity3d Input

    42/119

    Edit

    ProjectSettings

    Input

    N J

  • 8/10/2019 Unity3d Input

    43/119

    Name Jump

    Positive Button space

  • 8/10/2019 Unity3d Input

    44/119

    Input.anyKey, anyKeyDown

  • 8/10/2019 Unity3d Input

    45/119

    Input.anyKey, anyKeyDown

  • 8/10/2019 Unity3d Input

    46/119

    Input.anyKey//

    function Update() {if(Input.anyKey)

    Debug.Log("A key or mouse click has been

    detected");

    }

  • 8/10/2019 Unity3d Input

    47/119

    Input.anyKeyDown//()

    function Update() {if(Input.anyKeyDown)

    Debug.Log("A key or mouse click has been detected");

    }

  • 8/10/2019 Unity3d Input

    48/119

    inputString

  • 8/10/2019 Unity3d Input

    49/119

    inputString// Shows how to read typing input from the keyboard

    // (eg. the user entering his name).

    // You need to attach this script to a GUIText object.

    function Update () {for (var c : char in Input.inputString) {

    // Backspace - Remove the last character

    if (c == "\b"[0]) {if (guiText.text.Length != 0)

    guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);

    }else if (c == "\n"[0] || c == "\r"[0]) {// End of entry

    // "\n" for Mac, "\r" for windows.

    print ("User entered his name: " + guiText.text);

    }else {// Normal text input - just append to the end

    guiText.text += c;

    }

    }

    }

  • 8/10/2019 Unity3d Input

    50/119

    Input.GetKey(Down , Up)

  • 8/10/2019 Unity3d Input

    51/119

  • 8/10/2019 Unity3d Input

    52/119

  • 8/10/2019 Unity3d Input

    53/119

    Input

  • 8/10/2019 Unity3d Input

    54/119

    Input

  • 8/10/2019 Unity3d Input

    55/119

    Horizontal

  • 8/10/2019 Unity3d Input

    56/119

    Vertical

  • 8/10/2019 Unity3d Input

    57/119

    Fire1

  • 8/10/2019 Unity3d Input

    58/119

    Jump

    M X

  • 8/10/2019 Unity3d Input

    59/119

    Mouse X

    M S llWh l

  • 8/10/2019 Unity3d Input

    60/119

    Mouse ScrollWheel

    Wi d Sh k X

  • 8/10/2019 Unity3d Input

    61/119

    Window Shake X

    Horizontal

  • 8/10/2019 Unity3d Input

    62/119

    Horizontal

    Fire1

  • 8/10/2019 Unity3d Input

    63/119

    Fire1

    Jump

  • 8/10/2019 Unity3d Input

    64/119

    Jump

    Input GetAxis GetAxisRaw

  • 8/10/2019 Unity3d Input

    65/119

    Input.GetAxis, GetAxisRaw

    Input GetAxis

  • 8/10/2019 Unity3d Input

    66/119

    Input.GetAxis

    Input GetAxis

  • 8/10/2019 Unity3d Input

    67/119

    Input.GetAxis

    // A very simplistic car driving on the x-z plane.

    var speed : float = 10.0;

    var rotationSpeed : float = 100.0;

    function Update () {// Get the horizontal and vertical axis.

    // By default they are mapped to the arrow keys.

    // The value is in the range -1 to 1

    var translation : float = Input.GetAxis ("Vertical") * speed;

    var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;// Make it move 10 meters per second instead of 10 meters per frame...

    translation *= Time.deltaTime;

    rotation *= Time.deltaTime;

    // Move translation along the object's z-axis

    transform.Translate (0, 0, translation);

    // Rotate around our y-axis

    transform.Rotate (0, rotation, 0);

    }

    Input GetAxis

  • 8/10/2019 Unity3d Input

    68/119

    Input.GetAxis

    Input GetAxis

  • 8/10/2019 Unity3d Input

    69/119

    Input.GetAxis

    // Performs a mouse look.

    var horizontalSpeed : float = 2.0;var verticalSpeed : float = 2.0;

    function Update () {

    // Get the mouse delta. This is not in the range -1...1

    var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");

    var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");

    transform.Rotate (v, h, 0);

    }

    Input GetAxisRaw

  • 8/10/2019 Unity3d Input

    70/119

    Input.GetAxisRaw

    function Update () {var speed : float = Input.GetAxisRaw("Horizontal") * Time.deltaTime;

    transform.Rotate (0, speed, 0);

    }

    Input

  • 8/10/2019 Unity3d Input

    71/119

    Size 1825

    Input

  • 8/10/2019 Unity3d Input

    72/119

    Rename

    Input

  • 8/10/2019 Unity3d Input

    73/119

    Input

    Input.GetJoystickNames

  • 8/10/2019 Unity3d Input

    74/119

    Input.GetJoystickNames

    // Prints a joystick name if movement is detected.

    function Update () {// requires you to set up axes "Joy0X" - "Joy3X" and "Joy0Y"

    - "Joy3Y" in the Input Manger

    for (var i : int = 0; i < 4; i++) {

    if (Mathf.Abs(Input.GetAxis("Joy"+i+"X")) > 0.2

    || Mathf.Abs(Input.GetAxis("Joy"+i+"Y")) > 0.2)

    Debug.Log (Input.GetJoystickNames()[i]+" is moved");

    }

    }

  • 8/10/2019 Unity3d Input

    75/119

    (JoyStick)

  • 8/10/2019 Unity3d Input

    76/119

    Input.GetAxis("X axis")

    Input.GetAxisRaw("X axis")

    Input.GetButton("joystick button 0")

    Input.GetKey(KeyCode.Joystick1Button0)

  • 8/10/2019 Unity3d Input

    77/119

    KeyCode.JoystickButton0

  • 8/10/2019 Unity3d Input

    78/119

    Input.GetAxis

    Input.GetAxis (Vertical) ()

  • 8/10/2019 Unity3d Input

    79/119

    Input.GetAxis (Horizontal)() // A very simplistic car driving on the x-z plane. var speed : float = 10.0; var rotationSpeed : float = 100.0; function Update () { // Get the horizontal and vertical axis. // By default they are mapped to the arrow keys. // The value is in the range -1 to 1 var translation : float = Input.GetAxis ("Vertical") * speed; var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;

    // Make it move 10 meters per second instead of 10 meters per frame... translation *= Time.deltaTime; rotation *= Time.deltaTime;

    // Move translation along the object's z-axis transform.Translate (0, 0, translation);

    // Rotate around our y-axis transform.Rotate (0, rotation, 0); }

  • 8/10/2019 Unity3d Input

    80/119

  • 8/10/2019 Unity3d Input

    81/119

    var speed : float = 10.0; var rotationSpeed : float = 10.0; function Update () { if(Input.GetKey(KeyCode.JoystickButton0)) {

    transform.Rotate(5, 0, 0); print("up arrow key is held down"); } if(Input.GetKey("joystick button 1")) { transform.Rotate(0, 5, 0); print("up arrow key is held down"); } if(Input.GetKey("joystick button 2")) {

    transform.Rotate(0, 0, 5); print("up arrow key is held down"); }

    var translation : float = Input.GetAxis ("Vertical") * speed; var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed; // Make it move 10 meters per second instead of 10 meters per frame... translation *= 0.1; rotation *= 0.01; // Move translation along the object's z-axis transform.Translate (0, 0, translation); // Rotate around our y-axis transform.Translate (rotation, 0, 0); }

    http://wiki.etc.cmu.edu/unity3d/index.php/Joystick/Controller

  • 8/10/2019 Unity3d Input

    82/119

    p y p p y

    Arcade Stick for Sony Playstation 3

  • 8/10/2019 Unity3d Input

    83/119

    Microsoft Xbox 360 Controller

  • 8/10/2019 Unity3d Input

    84/119

  • 8/10/2019 Unity3d Input

    85/119

  • 8/10/2019 Unity3d Input

    86/119

    di j S i

  • 8/10/2019 Unity3d Input

    87/119

    EditProject SettingsInput

    Size

  • 8/10/2019 Unity3d Input

    88/119

  • 8/10/2019 Unity3d Input

    89/119

    I t G tK (K C d J ti k4B tt 0)

  • 8/10/2019 Unity3d Input

    90/119

    Input.GetKey(KeyCode.Joystick4Button0)

    I t G tA i R ("J2 3")

  • 8/10/2019 Unity3d Input

    91/119

    Input.GetAxisRaw("J2-3")

  • 8/10/2019 Unity3d Input

    92/119

    1,2,4Unity3d Joystick1,2,4(Joystick 3)

  • 8/10/2019 Unity3d Input

    93/119

    Mode

  • 8/10/2019 Unity3d Input

    94/119

    Mode

  • 8/10/2019 Unity3d Input

    95/119

  • 8/10/2019 Unity3d Input

    96/119

  • 8/10/2019 Unity3d Input

    97/119

  • 8/10/2019 Unity3d Input

    98/119

    AndroidBundle Identifier

  • 8/10/2019 Unity3d Input

    99/119

    Bundle Identifier

  • 8/10/2019 Unity3d Input

    100/119

    Bundle Identifier Bundle Identifierpackage namePackage Name

    com.myCompanyName.MyGame

    net.YourCompanyName.YourGame

    Bundle Identifier(Package Name)Android MarketPackageName

    Bundle Identifier(PackageName)com.mywebsite.mygameAndroid Market

    Bundle Version

    Bundle Code

    iPhoneUtils.Vibrate();

  • 8/10/2019 Unity3d Input

    101/119

    // Press button to vibrate

    function OnGUI() {if (GUI.Button(Rect(0, 10, 100, 32), "Vibrate!"))

    iPhoneUtils.Vibrate();

    }

  • 8/10/2019 Unity3d Input

    102/119

  • 8/10/2019 Unity3d Input

    103/119

  • 8/10/2019 Unity3d Input

    104/119

  • 8/10/2019 Unity3d Input

    105/119

    Input.GetTouch

  • 8/10/2019 Unity3d Input

    106/119

  • 8/10/2019 Unity3d Input

    107/119

  • 8/10/2019 Unity3d Input

    108/119

  • 8/10/2019 Unity3d Input

    109/119

  • 8/10/2019 Unity3d Input

    110/119

  • 8/10/2019 Unity3d Input

    111/119

  • 8/10/2019 Unity3d Input

    112/119

  • 8/10/2019 Unity3d Input

    113/119

  • 8/10/2019 Unity3d Input

    114/119

  • 8/10/2019 Unity3d Input

    115/119

  • 8/10/2019 Unity3d Input

    116/119

  • 8/10/2019 Unity3d Input

    117/119

    http://unity3d.com/support/documentation/Script

    Reference/Screen.html

  • 8/10/2019 Unity3d Input

    118/119

    // Start in landscape mode

  • 8/10/2019 Unity3d Input

    119/119

    // Start in landscape mode

    function Start () {Screen.orientation =

    ScreenOrientation.LandscapeLeft;

    }