Hi im new to unity game development im trying to make simple 2d character movement everthing is working fine like moving right,left but my character is jumping twice for some reason instead of single jump im also checking if player is on ground. below is my script for PlayerMovemnt
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Start is called before the first frame update
private bool moveRight = false;
private bool moveLeft = false;
private bool isjump = false;
public bool isOnGround = true;
public float jumpForce = 100f;
public float moveSpeed = 10f;
public Rigidbody2D Player;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("d"))
{
moveRight = true;
}
if (Input.GetKey("a"))
{
moveLeft = true;
}
if (Input.GetKey(KeyCode.Space))
{
isjump = true;
}
}
private void FixedUpdate()
{
if (moveRight)
{
moveRight = false;
// Player.AddForce(new Vector2(moveSpeed , 0), ForceMode2D.Force);
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
if (moveLeft)
{
moveLeft = false;
// Player.AddForce(new Vector2(-forwardForce , 0), ForceMode2D.Force);
transform.position -= Vector3.right * moveSpeed * Time.deltaTime;
}
if (isjump && isOnGround)
{
isjump = false;
// Player.AddForce(Vector2.up * jumpForce * Time.deltaTime ,ForceMode2D.Impulse);
Player.velocity = Vector2.up * jumpForce;
}
}
}
and this for ground check
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundedCheck : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("ground"))
{
FindObjectOfType<PlayerMovement>().isOnGround = true;
Debug.Log("joined");
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("ground"))
{
FindObjectOfType<PlayerMovement>().isOnGround = false;
Debug.Log("leaved");
}
}
}
i also tried OnCollisionEnter2D method but results was same.