How do I make the character move(left - right) smoothly while jumping in 2D game ?
I am making the game for school project with friends and I'm pretty new to working in unity and C#.
I've recently added movement to my game and I have problem with moving mid air. I want to make it more smooth, right know it looks like my character just glides. Also it kinda stops the jump if I move in one of directions. Its mobile game and its controlled with invisible buttons on screen.
Heres the movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Animator PlayerController;
Rigidbody2D playerRB;
bool canWalk = false;
bool walkRight;
bool isJumping = false;
float walkSpeed = 4f;
float jumpSpeed = 15f;
private void Start()
{
PlayerController = GetComponent<Animator>();
playerRB = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (canWalk && walkRight )
playerRB.velocity = transform.right * walkSpeed;
else if (canWalk && !walkRight )
{
playerRB.velocity = -transform.right * walkSpeed;
}
}
public void LeftWalk(bool value)
{
PlayerController.SetBool("walk", value);
transform.localScale = new Vector2(-1f, transform.localScale.y);
walkRight = false;
canWalk = value;
}
public void RighttWalk(bool value)
{
PlayerController.SetBool("walk", value);
transform.localScale = new Vector2(1f, transform.localScale.y);
walkRight = true;
canWalk=value;
}
public void Jump()
{
if (!isJumping)
{
isJumping = true;
PlayerController.SetTrigger("jump");
playerRB.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
}
}
private void OnCollisionEnter2D(Collision2D collsion)
{
if(collsion.collider.CompareTag("Ground"))
{
isJumping=false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
[C#] Can't Control Movement While Jumping 0 Answers
Air Control While Jumping? 1 Answer
2D Character movement using OnDrag 0 Answers
How to move character to touch Y location smoothly? 0 Answers