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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[C#] What am I doing wrong???

Discussion in 'Scripting' started by DBether, Dec 28, 2015.

  1. DBether

    DBether

    Joined:
    Sep 18, 2015
    Posts:
    15
    Hello! I have set up 3 2D animations - Idle; Walk; Jump;

    To have the Idle -> Walk transition happen the float parameter "Speed" needs to be greater than 0.1. I have that being set in my player movement script. Everything seems to work fine except the fact that the transition happens only when moving left even though the code is the same for both sides. Please take a look and help me out. ;)

    P.S. I already submitted the same post on the Animation forum but nobody was able to help me there.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Player : MonoBehaviour {
    4.      public float speed = 1f;
    5.      public float jumpHeight = 100f;
    6.      public bool onGround = true;
    7.      public Transform jumpCheckEnd;
    8.      Animator animator;
    9.      void Start() {
    10.          animator = GetComponent<Animator>();
    11.      }
    12.      void Update() {
    13.          if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow)) {
    14.              transform.Translate (Vector2.right * speed * Time.deltaTime);
    15.              transform.GetComponent<SpriteRenderer> ().flipX = false;
    16.              animator.SetFloat ("Speed", 1f);
    17.          } else {
    18.              animator.SetFloat ("Speed", 0f);
    19.          }
    20.          if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow)) {
    21.              transform.Translate (Vector2.left * speed * Time.deltaTime);
    22.              transform.GetComponent<SpriteRenderer> ().flipX = true;
    23.              animator.SetFloat("Speed", 1f);
    24.          } else {
    25.              animator.SetFloat("Speed", 0f);
    26.          }
    27.          Debug.DrawLine(this.transform.position, jumpCheckEnd.position, Color.white);
    28.          onGround = Physics2D.Linecast(this.transform.position, jumpCheckEnd.position, 1 << LayerMask.NameToLayer("Ground"));
    29.          if (Input.GetKeyDown(KeyCode.Space) && onGround == true) {
    30.              GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpHeight);
    31.          }
    32.          if (onGround == true) {
    33.              animator.SetBool("onGround", true);  
    34.          } else {
    35.              animator.SetBool("onGround", false);
    36.          }
    37.      }
    38. }
     
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,

    If the "Speed" is set to 1 in "Right code", you reset it to 0 in the "Left code". You can do something like :
    Code (csharp):
    1.  
    2.          if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow)) {
    3.              transform.Translate (Vector2.right * speed * Time.deltaTime);
    4.              transform.GetComponent<SpriteRenderer> ().flipX = false;
    5.             animator.SetFloat ("Speed", 1f);
    6.          }
    7.          else if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow)) {
    8.              transform.Translate (Vector2.left * speed * Time.deltaTime);
    9.              transform.GetComponent<SpriteRenderer> ().flipX = true;
    10.              animator.SetFloat("Speed", 1f);
    11.          } else {
    12.              animator.SetFloat("Speed", 0f);
    13.          }
    14.  
     
  3. DBether

    DBether

    Joined:
    Sep 18, 2015
    Posts:
    15
    Thank you so much! This actually worked. You're the first person to even try to help me. You're awesome. ;)