Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Adding the animation is not working! 2d .

Discussion in 'Scripting' started by JKMS, May 4, 2015.

  1. JKMS

    JKMS

    Joined:
    Nov 1, 2014
    Posts:
    37
    I watched the 2d character controller video from the learn section and completed it , now i got a crouch animation , actually I dont know scripting . I tried many ways to do it but some errors will come .

    So i have a crouch and crouch walking animation , so if i press the c button , the crouch animation will play and if I press the walk right/left button (a,d) the crouch walking animation goto that position . So can you explain me what to do , I mean not only the script , also explain me what i have to do .


    Heres the script i use to run the character .

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RobotControllerScript : MonoBehaviour {
    6.  
    7.     public float maxSpeed = 10.0f;
    8.     bool facingRight = true;
    9.     Animator anim;
    10.  
    11.     public bool grounded = false;
    12.     public Transform groundCheck;
    13.     float groundRadius = 0.2f;
    14.     public LayerMask whatIsGround;
    15.     public float jumpForce = 700f;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         anim = GetComponent<Animator> ();
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void FixedUpdate () {
    24.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    25.         anim.SetBool ("Ground", grounded);
    26.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    27.  
    28.         float move = Input.GetAxis ("Horizontal");
    29.         anim.SetFloat ("Speed", Mathf.Abs(move));
    30.  
    31.         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    32.  
    33.         if (move > 0 && !facingRight)
    34.             Flip ();
    35.         else if (move < 0 && facingRight)
    36.             Flip ();
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.         if (grounded && Input.GetKey (KeyCode.Space))
    42.         {
    43.             anim.SetBool("Ground", false);
    44.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    45.         }
    46.     }
    47.  
    48.     void Flip()
    49.     {
    50.         facingRight = !facingRight;
    51.         Vector3 theScale = transform.localScale;
    52.         theScale.x *= -1;
    53.         transform.localScale = theScale;
    54.     }
    55. }
    56.  
    57.  
    Would be appreciated if you help me .
     
    Last edited: May 4, 2015
  2. JKMS

    JKMS

    Joined:
    Nov 1, 2014
    Posts:
    37
    Bump
     
  3. JKMS

    JKMS

    Joined:
    Nov 1, 2014
    Posts:
    37
    Ok i made a script , now the problem i press the jump key ( space button ) 3 times it jumps 3 times , if i press it 100 times , it jumps 100 times , i need to jump only 2 times , how can i fix it.