Player sometimes doesnt jump Instantly when clicked
I made a platformer game, where the player has to reach the end to pass the level. The player is a SPHERE BALL. My problem is when the ball roles it sometimes does not jump when the screen is clicked. I tried putting the player movement into void FixedUpdate(), instead of void Update(). The player still doesnt jump instantly, i checked all my ground colliders and there isnt any problems. Im not sure what the problem is. Any ideas? Many thanks
using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;
public class PlayerControl : MonoBehaviour
{
//COMPONENTS
private Rigidbody2D myRigidbody;
private CircleCollider2D myCollider;
//SPEED/JUMP
public float moveSpeed;
private float moveSpeedStore;
public float jumpForce;
public float jumpTime;
private float jumpTimeCounter;
private bool stoppedJumping;
private bool canDoubleJump;
//GROUNDED
public bool grounded;
public LayerMask whatIsGround;
// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myCollider = GetComponent<CircleCollider2D>();
speedMilestoneCount = speedIncreaseMilestone;
moveSpeedStore = moveSpeed;
speedMilestoneCountStore = speedMilestoneCount;
speedIncreaseMilestoneStore = speedIncreaseMilestone;
jumpTimeCounter = jumpTime;
stoppedJumping = true;
}
void FixedUpdate()
{
grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
/*grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);*/
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
if (Input.GetMouseButtonDown(0))
{
if (grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
stoppedJumping = false;
}
if (!grounded && canDoubleJump)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter = jumpTime;
stoppedJumping = false;
canDoubleJump = false;
}
}
if ((Input.GetMouseButton(0)) && !stoppedJumping)
{
if (jumpTimeCounter > 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
}
if (Input.GetMouseButtonUp(0))
{
jumpTimeCounter = 0;
stoppedJumping = true;
}
if (grounded)
{
jumpTimeCounter = jumpTime;
canDoubleJump = true;
}
}
// Update is called once per frame
void Update()
{
//SPEED MULTIPLIER CODE HERE
}
Answer by Segy · Jan 25, 2016 at 04:45 PM
Found the solution.
grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
I left the above two in void fixedupdate, and moved the rest in void update.
Your answer
Follow this Question
Related Questions
Physics2D.Raycast not working with LayerMask 0 Answers
Using OnTriggerStay for Ground Detection 0 Answers
Ground check doesnt work 0 Answers