Problems with player jumping (Welcome to Unity Answers The best place to ask and answer questions )
Hi All I have 3 problems with the player jumping :
Problem 1 :
When i'm holding button down character jump at to heights randomly , one is little bit higher than the basic jump another high as planned.
Problem 2 :
when i'm running and colliding with an object (Box) and jumping the player make a big height jump !!
Problem 3 :
when my player jump and i'm holding button Run in air and colliding with an object (in air) ,my player Keep clinging to the object Until leave the running button
This is my C# Code :
public class PlayerController2D : MonoBehaviour { public float speedForce = 5f; Animator anim; Rigidbody2D myRB; bool facingRight;
public bool isGrounded;
public Transform point1;
public Transform point2;
public LayerMask onlyGroundMask;
public float jumpForce;
public float timer;
bool canJump;
public float maxTime = 0.1f;
// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
anim.SetBool("Grounded", isGrounded);
anim.SetFloat("velocityY", myRB.velocity.y);
float move = Input.GetAxis("Horizontal");
myRB.velocity = new Vector2(move * speedForce, myRB.velocity.y);
anim.SetFloat("Astate", Mathf.Abs(move));
if (move < 0 && !facingRight) Flip();
else if (move > 0 && facingRight) Flip();
//Jump
isGrounded = Physics2D.OverlapArea(point1.position, point2.position, onlyGroundMask);
if (isGrounded && Input.GetAxis("Jump") > 0)
{
timer = 0;
canJump = true;
myRB.AddForce(new Vector2(0,jumpForce * 3));
}else if (isGrounded && Input.GetAxis("Jump") > 0 && canJump && timer < maxTime)
{
timer += Time.deltaTime;
myRB.AddForce(new Vector2(0, jumpForce));
}else
{
canJump = false;
}
}
// @julian_at_pf @UnbreakableOne @zri5004 @Artic @Evgenij @fabian-mkv @Cepheid @Ymrasu @DThaiPome @MarkTerence
Your answer
Follow this Question
Related Questions
2D Movement Controls 1 Answer
Jump Script 2 Answers
How do I set the jump distance and height to my player? 0 Answers
I need help with AI,Force not working 0 Answers
Randomize text position for 2D Quiz C# 0 Answers