- Home /
why player jumps forever
I have been watching the 2D Character Controllers tutorial. I set up everything like he did using my own sprites. I ran into a problem when adding jump. He adds a game object to detect ground which worked okay at first. I got the check mark when on the ground and when I fell, it went away. So I tried setting up jump with a blend tree to smooth animation. When I did this, some how player no longer detected the ground. Player would also jump with out me hitting space bar. He would also jump forever. So I changed the code and just made the animation normal. After that everything worked fine. This was at midnight last night. Saved closed everything and went to bed. When I got up everything I did was still the same. But When I hit play the player jumps again without hitting space bar forever. very strange and frustrating any help would be appreciated.
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
public float jumpforce =700f;
public float maxSpeed = 10f;
bool faceRight = true;
Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate ()
{
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && ! faceRight)
flip ();
else if (move < 0 && faceRight)
flip ();
}
void Update()
{
if( Input.GetKeyDown(KeyCode.Space));
{
rigidbody2D.AddForce(new Vector2( 0, jumpforce));
}
}
void flip ()
{
faceRight = !faceRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Answer by Yusaku · Jan 19, 2014 at 07:56 PM
does the jump anuimation play forever or does the character continue to climb vertically forever?
Both actually animation keeps looping and character goes up forever never falls.
What is weird when I saved last night it worked! I have not changed anything since then. I thought it was the ground detection doing it. That is why I removed it and changed the code, to the way it is now. It worked but after closing and saving some how it revert back to doing the same thing.
Ok im pretty new at unity and development myself so I might just be missing something or maybe it not in this part of ur code but where are u implementing gravity? are you using rigid body gravity? ,ake sure if u are its still checked. You might try reloading your game too. Unity is wierd sometimes. I had a bit of harmless code the other day delete a mesh from my game. not an istance $$anonymous$$d you but the actual object had to reimport it.
Answer by QuestionAsker · Jan 19, 2014 at 08:44 PM
I can't really tell too well whats going on, but I'd bet your issue is in lines 36-40 or your flip function. Or another script. Sometimes however, Unity acts weird and closing it all out, or even a restart will fix it. It's rare for an issue like that, but if it did infact work the night before, and you haven't changed anything, Id say reboot. It's happened to me. Good luck!
Your answer
Follow this Question
Related Questions
How can I improve 2D Jump? because it's sluggish 1 Answer
Jumping effectively in a 2D sidescroller 0 Answers
how to control amount of force taken from the surface 0 Answers
OnCollisonEnter2D. 1 Answer