I am new to Unity, and am trying to make a simple 2D platformer from this video, and I am trying to modify it to prevent the player from being able to switch directions during a jump. Right now, you can jump and then move. I want to freeze the left/right player controls and give control of the players location FULLY to the rigidbody. Can anyone help? I can update with any additional info y'all need.
Here is my player controller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
public float speed;
public float jumpForce;
private float moveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
private bool upKeyPressed;
private bool upKeyHeld;
private bool upKeyUp;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
void Update() {
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
upKeyPressed = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow);
upKeyHeld = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);
upKeyUp = Input.GetKeyUp(KeyCode.W) && Input.GetKeyUp(KeyCode.UpArrow);
if(moveInput > 0 && isGrounded == true) {
transform.eulerAngles = new Vector3(0, 0, 0);
} else if(moveInput < 0 && isGrounded == true) {
transform.eulerAngles = new Vector3(0, 180, 0);
}
if(isGrounded == true && upKeyPressed) {
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if(upKeyHeld && isJumping == true) {
if(jumpTimeCounter > 0) {
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
} else {
isJumping = false;
}
}
if(Input.GetKeyUp(KeyCode.UpArrow)) {
isJumping = false;
}
}
}
```