Hello I wrote a player script to move and my main problem is I can’t walk and jump simultaneously my script is inspired from others but when I press right or left it doesn’t jump on the contrary when I jump I can move left or right but I’m falling slower than when not moving. (rb angular set to 0 gravity to 1) And the animation takes time to play even when exit time is 0
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
public float walkSpeed = 10f;
private BoxCollider2D[] boxes;
public BoxCollider2D me;
public BoxCollider2D weapon;
private bool jump;
private bool attack;
public Animator playerAnimator;
private Vector2 moveVelocity;
private Vector2 moveVelocityj;
public float jumpHeight = 10f;
public PlayerManager playerManager;
private void Start()
{
playerAnimator = GetComponentInChildren<Animator>();
rb = GetComponent<Rigidbody2D>();
boxes = GetComponents<BoxCollider2D>();
for (int i = 0; i < boxes.Length; i++)
{
me = boxes[0];
weapon = boxes[1];
}
}
// Update is called once per frame
private void Update()
{
if (Input.GetButtonDown("Jump") && isGrounded())// sur place
{
playerAnimator.SetBool("Walk", false);
playerAnimator.SetTrigger("Jump");
jump = true;
Vector2 vector2j = new Vector2(0, rb.position.y + jumpHeight);
moveVelocityj = vector2j * jumpHeight;
//rb.MovePosition(rb.position + moveVelocityj * Time.deltaTime);
rb.velocity=(Vector2.up*jumpHeight);
}
if (Input.GetButtonUp("Jump") && !isGrounded())
{
jump = false;
playerAnimator.SetBool("Walk", false);
}
Vector2 vector2 = new Vector2(Input.GetAxisRaw("Horizontal"), 0);
if ((vector2.x !=0) && (playerManager.death!=true))
{ playerAnimator.SetBool("Walk",true);
moveVelocity = vector2 * walkSpeed;
Vector2 localRot = new Vector2(0f, (Input.GetAxisRaw("Horizontal") <= 0) ? 180f : 0f);
transform.eulerAngles = localRot;
rb.MovePosition(rb.position + moveVelocity * Time.deltaTime);
}
if (!isGrounded()||vector2.x==0)
{
playerAnimator.SetBool("Walk", false);
}
if (Input.GetButtonDown("Jump") && isGrounded() && vector2.x!=0)// sur place
{
Vector2 localRot = new Vector2(0f, (Input.GetAxisRaw("Horizontal") <= 0) ? 180f : 0f);
transform.eulerAngles = localRot;
rb.MovePosition(rb.position + moveVelocity * Time.deltaTime);
playerAnimator.SetBool("Walk", false);
playerAnimator.SetTrigger("Jump");
jump = true;
Vector2 vector2j = new Vector2(0, rb.position.y + jumpHeight);
moveVelocityj = vector2j * jumpHeight;
//rb.MovePosition(rb.position + moveVelocityj * Time.deltaTime);
rb.velocity = (Vector2.up * jumpHeight);
moveVelocity = vector2 * walkSpeed;
}
if (Input.GetButtonDown("Fire1"))
{ playerAnimator.SetTrigger("Attack");
playerAnimator.SetBool("Walk", false);
attack = true;
weapon.enabled = true;
//animation attack
}
if (Input.GetButtonUp("Fire1"))
{
playerAnimator.SetBool("Walk", false);
attack = false;
weapon.enabled = false;
//animation attack
}
if (playerManager.death)
{
playerAnimator.SetBool("Walk", false);
playerAnimator.SetTrigger("Death");
}
}
private bool isGrounded()
{
if(transform.position.y<=0.0f)
{
return true;
}
return false;
}