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

FIXED

Discussion in 'Scripting' started by Kameron-Schwab, Nov 14, 2015.

  1. Kameron-Schwab

    Kameron-Schwab

    Joined:
    Sep 16, 2015
    Posts:
    33
    So, what I mean is I've coded a script basically for a Crossy Road remake and now I made an Animation in unity for Idle and Jump so the idle works fine until I move. So, when I'm just idle not moving my Idle animation works but when I move for the first time it Continuously jumps?


    Bounce Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bounce : MonoBehaviour {
    5.  
    6.     float lerpTime;
    7.     float currentLerpTime;
    8.     float perc = 1;
    9.  
    10.     Vector3 startpos;
    11.     Vector3 endpos;
    12.  
    13.     bool firstInput;
    14.     public bool justJump;
    15.  
    16.     void Update()
    17.     {
    18.         if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right"))
    19.         {
    20.             if (perc == 1)
    21.             {
    22.                 lerpTime = 1;
    23.                 currentLerpTime = 0;
    24.                 firstInput = true;
    25.                 justJump = true;
    26.             }
    27.         }
    28.         startpos = gameObject.transform.position;
    29.         if (Input.GetButtonDown("right") && gameObject.transform.position == endpos)
    30.         {
    31.             endpos = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
    32.         }
    33.         if (Input.GetButtonDown("left") && gameObject.transform.position == endpos)
    34.         {
    35.             endpos = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
    36.         }
    37.         if (Input.GetButtonDown("up") && gameObject.transform.position == endpos)
    38.         {
    39.             endpos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
    40.         }
    41.         if (Input.GetButtonDown("down") && gameObject.transform.position == endpos)
    42.         {
    43.             endpos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
    44.         }
    45.         if (firstInput == true)
    46.         {
    47.             currentLerpTime += Time.deltaTime * 5;
    48.             perc = currentLerpTime / lerpTime;
    49.             gameObject.transform.position = Vector3.Lerp(startpos, endpos, perc);
    50.             if (perc > 0.8)
    51.             {
    52.                 perc = 1;
    53.             }
    54.             if (Mathf.Round(perc) == 1)
    55.             {
    56.                 justJump = false;
    57.             }
    58.  
    59.         }
    60.     }
    61. }
    Animation Controller:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnimationController : MonoBehaviour {
    5.  
    6.     Animator anim;
    7.     public GameObject thePlayer;
    8.  
    9.     void Start ()
    10.     {
    11.         anim = gameObject.GetComponent<Animator> ();
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         Bounce bounceScript = thePlayer.GetComponent<Bounce> ();
    17.         if (bounceScript.justJump == true)
    18.         {
    19.             anim.SetBool ("Jump", true);
    20.         }
    21.         else
    22.         {
    23.             anim.SetBool("jump",false);
    24.         }
    25.     }
    26. }
     
  2. Kameron-Schwab

    Kameron-Schwab

    Joined:
    Sep 16, 2015
    Posts:
    33
  3. Kameron-Schwab

    Kameron-Schwab

    Joined:
    Sep 16, 2015
    Posts:
    33
    Fixed. Forgot my script was Sensitive to capitalization