Hey, everyone. It’s my first time posting to the Unity forums, so I’ll make this short and sweet: I would really like some help. I was working along on my script and encountered three issues. The first was that if I hit the “Jump” key quickly enough in succession I can infinitely jump. The second was to do with my Animator not telling me what variables were changing and when, and the final was that when I crouch my animation continues on a loop even if I let go of the crouch button (but this only occurs if I hold it down for more than a second). I have to tap the crouch key again to make the animation switch back to idle.
Script: (I apologize for it not being in proper forum format but I’m pressed for time)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
public float jumpForce = 50f;
public float normalfall = 2f;
public float fastfall = 9f;
bool grounded;
void Start ()
{
anim = GetComponent();
this.rigidbody2D.gravityScale = normalfall;
}
void Update ()
{
}
void FixedUpdate ()
{
anim.SetFloat (“vSpeed”, rigidbody2D.velocity.y);
if (grounded = true)
{
if (Input.GetButton (“Vertical”))
anim.SetBool (“Crouch”, true);
if (Input.GetButtonUp (“Vertical”))
anim.SetBool (“Crouch”, false);
}
else
{
if (Input.GetButton (“Vertical”))
this.rigidbody2D.gravityScale = fastfall;
if (Input.GetButtonUp (“Vertical”))
this.rigidbody2D.gravityScale = normalfall;
}
float move = Input.GetAxis (“Horizontal”);
anim.SetFloat (“Speed”, Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
if (grounded && Input.GetButtonDown (“Jump”))
{
anim.SetBool (“grounded”, false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
grounded = false;
}
void OnCollisionStay ()
{
grounded = true;
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}