Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnKeyDown play animation

Discussion in 'Scripting' started by jorn818, Oct 3, 2016.

  1. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97
    so I wrote a script for unityscript that plays animations on key press/input, i wanted this to become a C# script and according to the scripting api, just changing function to void should be enough, and yet the code doesnt work and gives countless of quite weird errors, for example certain symbols ''()'' which im pretty sure should be in the script
    this is the script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AnimSoldier : MonoBehaviour {
    6.  
    7.     function void ()
    8.     {
    9.         if (Input.GetKeyDown(KeyCode.Q))
    10.         {
    11.             GetComponent.< UnityEngine.Animation > ().Play("LeanLeft");
    12.  
    13.         }
    14.         if (Input.GetKeyDown(KeyCode.A))
    15.         {
    16.             GetComponent.< UnityEngine.Animation > ().Play("WalkLeft");
    17.         }
    18.         if (Input.GetKeyDown(KeyCode.D))
    19.         {
    20.             GetComponent.< UnityEngine.Animation > ().Play("WalkRight");
    21.         }
    22.         if (Input.GetKey(KeyCode.Space))
    23.         {
    24.             GetComponent.< UnityEngine.Animation > ().Play("JUMP");
    25.         }
    26.         if (Input.GetKeyUp(KeyCode.Space))
    27.         {
    28.             GetComponent.< UnityEngine.Animation > ().Play("soldieridle");
    29.         }
    30.         if (Input.GetKeyUp(KeyCode.Q))
    31.         {
    32.             GetComponent.< UnityEngine.Animation > ().Play("soldieridle");
    33.         }
    34.         if (Input.GetKeyUp(KeyCode.E))
    35.         {
    36.             GetComponent.< UnityEngine.Animation > ().Play("soldieridle");
    37.         }
    38.         if (Input.GetKeyDown(KeyCode.E))
    39.         {
    40.             GetComponent.< UnityEngine.Animation > ().Play("LeanRight");
    41.         }
    42.         if (Input.GetKeyDown(KeyCode.W))
    43.         {
    44.             GetComponent.< UnityEngine.Animation > ().Blend("WalkFor");
    45.             GetComponent.< UnityEngine.Animation > ().Blend("Soldierrunidle");
    46.         }
    47.         if (Input.GetKeyDown(KeyCode.S))
    48.         {
    49.             GetComponent.< UnityEngine.Animation > ().Blend("WalkBackw");
    50.         }
    51.         if (Input.GetKeyDown(KeyCode.W && KeyCode.LeftShift))
    52.         {
    53.             GetComponent.< UnityEngine.Animation > ().Play("SoldierRunArms");
    54.         }
    55.  
    56.         if (Input.GetMouseButtonDown(0))
    57.         {
    58.             GetComponent.< UnityEngine.Animation > ().Play("Recoilsoldier");
    59.  
    60.         }
    61.         else if (!Input.anyKey)
    62.         {
    63.             GetComponent.< UnityEngine.Animation > ().Play("soldieridle");
    64.         }
    65.  
    66.  

    any help?
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    function void
    to
    void Update

    GetComponent.<
    To
    GetComponent<
     
    jorn818 likes this.
  3. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97

    omg.. how embarresed i am for typing function void xD, then again i was very tired when writing this

    didnt know the getcomponent thing though so learned another thing today!

    Thanks ThermalFusion