C
C#15mo ago
pigeon8473

❔ Little question about c# in UNITY

Here is my PlayerMovement code and i have an issue, when i try to jump it doesn't works everytime i press my Spacebar and idk why. It works like every other time and it's annoying.
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;

public bool isJumping;
public bool isGrounded;

public Transform groundCheckLeft;
public Transform groundCheckRight;

public Rigidbody2D rb;
private Vector3 velocity = Vector3.zero;

void FixedUpdate()
{
isGrounded = Physics2D.OverlapArea(groundCheckLeft.position, groundCheckRight.position);

float horizontalMovement = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;

if(Input.GetButtonDown("Jump") && isGrounded == true)
{
isJumping = true;
}

MovePlayer(horizontalMovement);
}

void MovePlayer(float _horizontalMovement)
{
Vector3 targetVelocity = new Vector2(_horizontalMovement, rb.velocity.y);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, .05f);

if(isJumping == true)
{
rb.AddForce(new Vector2(0f, jumpForce));
isJumping = false;
}
}
}
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;

public bool isJumping;
public bool isGrounded;

public Transform groundCheckLeft;
public Transform groundCheckRight;

public Rigidbody2D rb;
private Vector3 velocity = Vector3.zero;

void FixedUpdate()
{
isGrounded = Physics2D.OverlapArea(groundCheckLeft.position, groundCheckRight.position);

float horizontalMovement = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;

if(Input.GetButtonDown("Jump") && isGrounded == true)
{
isJumping = true;
}

MovePlayer(horizontalMovement);
}

void MovePlayer(float _horizontalMovement)
{
Vector3 targetVelocity = new Vector2(_horizontalMovement, rb.velocity.y);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, .05f);

if(isJumping == true)
{
rb.AddForce(new Vector2(0f, jumpForce));
isJumping = false;
}
}
}
1 Reply
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.