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:
using UnityEngine;
using System.Collections;
public class Bounce : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;
Vector3 startpos;
Vector3 endpos;
bool firstInput;
public bool justJump;
void Update()
{
if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right"))
{
if (perc == 1)
{
lerpTime = 1;
currentLerpTime = 0;
firstInput = true;
justJump = true;
}
}
startpos = gameObject.transform.position;
if (Input.GetButtonDown("right") && gameObject.transform.position == endpos)
{
endpos = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
}
if (Input.GetButtonDown("left") && gameObject.transform.position == endpos)
{
endpos = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
}
if (Input.GetButtonDown("up") && gameObject.transform.position == endpos)
{
endpos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
}
if (Input.GetButtonDown("down") && gameObject.transform.position == endpos)
{
endpos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
}
if (firstInput == true)
{
currentLerpTime += Time.deltaTime * 5;
perc = currentLerpTime / lerpTime;
gameObject.transform.position = Vector3.Lerp(startpos, endpos, perc);
if (perc > 0.8)
{
perc = 1;
}
if (Mathf.Round(perc) == 1)
{
justJump = false;
}
}
}
}
Animation Controller:
using UnityEngine;
using System.Collections;
public class AnimationController : MonoBehaviour {
Animator anim;
public GameObject thePlayer;
void Start ()
{
anim = gameObject.GetComponent<Animator> ();
}
void Update ()
{
Bounce bounceScript = thePlayer.GetComponent<Bounce> ();
if (bounceScript.justJump == true)
{
anim.SetBool ("Jump", true);
}
else
{
anim.SetBool("jump",false);
}
}
}