How to stop my character from infinite jumping?
I am a new learner and i know almost nothing much about C# scripting,i have been following up with videos to make a 2D game but the only problem i faced so far is this,i cant stop my character from infinite jumping,how can i make it jump only when it touches the ground? here is my script i hope you can edit somehow that will get fixed
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
//Floats
public float maxspeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
//Booleans
public bool grounded;
//Stats
public int curHealth;
public int maxHealth = 5;
//References
private Rigidbody2D rb2d;
private Animator anim;
void Start()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
curHealth = maxHealth;
}
void Update()
{
anim.SetBool("Grounded",grounded);
anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
if (Input.GetAxis("Horizontal") < -0.1f)
{
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetAxis("Horizontal") > 0.1f)
{
transform.localScale = new Vector3(1, 1, 1);
}
if (Input.GetButtonDown("Jump") && grounded)
{
rb2d.AddForce(Vector2.up * jumpPower);
}
if (curHealth > maxHealth)
{
curHealth = maxHealth;
}
if(curHealth <= 0)
{
Die();
}
}
void FixedUpdate()
{
Vector3 easeVelocity = rb2d.velocity;
easeVelocity.y = rb2d.velocity.y;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.75f;
float h = Input.GetAxis("Horizontal");
//Fake friction / Easing the x speed of our player
if(grounded)
{
rb2d.velocity = easeVelocity;
}
//Moving the player
rb2d.AddForce((Vector2.right * speed) * h);
//Limiting the speed of the player
if(rb2d.velocity.x >maxspeed)
{
rb2d.velocity = new Vector2(maxspeed, rb2d.velocity.y);
}
if(rb2d.velocity.x < -maxspeed)
{
rb2d.velocity = new Vector2(-maxspeed, rb2d.velocity.y);
}
}
void Die()
{
//Restart
Application.LoadLevel(Application.loadedLevel);
}
public void Damage (int Damage)
{
curHealth -= Damage;
gameObject.GetComponent<Animation>().Play("Player_RedFlash");
}
public IEnumerator Knockback(float knockDur, float knockbackPwr, Vector3 knockbackDir) {
float timer = 0;
while( knockDur > timer)
{
timer+=Time.deltaTime;
rb2d.AddForce(new Vector3(knockbackDir.x * -100, knockbackDir.y * knockbackPwr, transform.position.z));
}
yield return 0;
}
}
Answer by Lukamyte · Mar 05, 2016 at 12:54 AM
Hello Ameer.
if(Physics2D.Linecast(transform.position, transform.position - Vector2(0, -1), 1 << LayerMask.NameToLayer("Terrain"))){
grounded = true;
}else if(Physics2D.Linecast(transform.position, transform.position - Vector2(0.33, -1), 1 << LayerMask.NameToLayer("Terrain"))){
grounded = true;
}else if(Physics2D.Linecast(transform.position, transform.position - Vector2(-0.33, -1), 1 << LayerMask.NameToLayer("Terrain"))){
grounded = true;
}else{
grounded = false;
}
This should work for you. Explanation of what it does: Each of these IF statements casts a small line below the player to check and see if there is anything below it. If there IS terrain below it, it will respond as being "grounded". http://docs.unity3d.com/ScriptReference/30_search.html?q=physics2D.linecast is the documentation for this statement.
The 1 << LayerMask.NameToLayer("Terrain")
Makes sure that the line only returns true if it's colliding with the ground, and not the player. You will need to make a new layer in the Editor and assign it to the terrain to make this work. The name of the layer will need to be the same as it is in the code.
Final thing: I put this linecast 3 times so that even if the player is not standing directly over the ground, like if there was an edge, it would still allow him to jump.
I hope this helps. Sorry if I accidentally wrote this in JavaScript. ; )
i managed to get rid of that error by changing 'LineCast' to 'Linecast' and now i get several errors in Vector2,so im pretty much stuck right now
i managed to get rid of the error before but now im getting few error with Vector2 and i still dont know why
Turns out I had one more right parentheses than was needed at the end of those if statements. I changed it in the answer to fit. So, if you just copy the code above again, it has the revisions you stated you made and the ones I've made too. I hope this is the only issue you were having.
Your answer
Follow this Question
Related Questions
Player Is infinite jumping 1 Answer
how to stop infinite jumping in air? 0 Answers
Hello! I have infinite jump problem, please help me! 0 Answers