- Home /
Question by
tajwelljones · May 03, 2015 at 09:41 PM ·
playerjumpjumping
Why does my character never stop jumping?
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour { public float maxSpeed = 10f; bool facingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip();
}
void Update ()
{
if (grounded && Input.GetKeyDown(KeyCode.Space));
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
}
void Flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Comment
Answer by HD32 · May 04, 2015 at 10:21 AM
Make sure that on the animation configuration, "loop" is not checked. Also on line 11 of your script, you must insert the name of the animator you will be using for the jump animation. If all else fails consider setting your grounded variable to true, I am not good with C# but that could work.
Thank you very much for your help, it means a lot that you took the time to try and help. Unfortunately none of your options worked for me, if anything, i will just start over on the script.