While coding a 2D movement for my game in Unity I got myself into an issue. Previously I took the decision to make so the character can't move in the middle of the air, however, I'd like to change that. How can I do it? I want the character to be able to turn direction in the middle of the air, but in a different speed than "moveSpeed". This is my first time on this website, so I apologize if I let out too many details. Here are the movement and jump scripts:
WALK SCRIPT
public bool canMove = true;
[SerializeField] public Vector2 newVelocity;
[SerializeField] public Rigidbody2D rb;
[SerializeField] public float moveSpeed = 10f;
[SerializeField] public Vector2 direction;
[SerializeField] public float wallSlideSpeed;
public float movementForceAir;
public float airDragMultiplier;
FlipChar flipc;
GroundCheck gc;
SlopeWalk sw;
PlayerJump pj;
WallChecker wc;
DashAfterimage da;
void Awake()
{
gc = GetComponent<GroundCheck>();
sw = GetComponent<SlopeWalk>();
rb = GetComponent<Rigidbody2D>();
pj = GetComponent<PlayerJump>();
wc = GetComponent<WallChecker>();
da = GetComponent<DashAfterimage>();
}
void Start()
{
flipc = GetComponent<FlipChar>();
}
void Update()
{
}
void FixedUpdate()
{
if (gc.onGround && canMove && !da.dashing)
{
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, rb.velocity.y);
}
else if (!gc.onGround && !wc.isWallSliding && Input.GetAxis("Horizontal") != 0)
{
Vector2 forceToAdd = new Vector2(movementForceAir * Input.GetAxis("Horizontal"), 0);
rb.AddForce(forceToAdd);
if (Mathf.Abs(rb.velocity.x) > moveSpeed)
{
rb.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), rb.velocity.y);
// }else if(!gc.onGround && !wc.isWallSliding && Input.GetAxis("Horizontal") == 0){
// rb.velocity = new Vector2(rb.velocity.x , rb.velocity.y);
}
}
if (wc.isWallSliding)
{
if (rb.velocity.y < wallSlideSpeed)
{
rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
}
}
}
}
}
JUMP SCRIPT
[HideInInspector] public GroundCheck gc;
[HideInInspector] public PlayerWalk pw;
[SerializeField] private float jumpForce;
[SerializeField] private float jumpTimeCounter;
[SerializeField] private float jumpTime;
[SerializeField] private float extraJumps;
[SerializeField] private float extraJumpsValue;
[SerializeField] private float variableJumpHeight = 0.5f;
public bool isJumping;
public bool canJump;
WallJumpCode wjc;
WallChecker wc;
FlipChar fc;
DashAfterimage da;
void Start()
{
wjc = GetComponent<WallJumpCode>();
pw = GetComponent<PlayerWalk>();
gc = GetComponent<GroundCheck>();
fc = GetComponent<FlipChar>();
extraJumps = extraJumpsValue;
da = GetComponent<DashAfterimage>();
wc = GetComponent<WallChecker>();
}
void Update()
{
isJumping = true;
if (pw.rb.velocity.y <= 0.0f)
{
isJumping = false;
}
if ((gc.onGround && pw.rb.velocity.y <= 0.0f) || wc.isWallSliding)
{
extraJumps = extraJumpsValue;
}
if ((Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && !wc.isWallSliding))
{
pw.rb.velocity = new Vector2(pw.rb.velocity.x, jumpForce);
extraJumps--;
}
else if (wc.isWallSliding && Input.GetKeyDown(KeyCode.Space) && !isJumping)
{
wc.isWallSliding = false;
extraJumps--;
Vector2 forceToAdd = new Vector2(wjc.wallHopForce * wjc.wallHopDirection.x * -wjc.facingDirection, wjc.wallHopForce * wjc.wallHopDirection.y);
pw.rb.AddForce(forceToAdd, ForceMode2D.Impulse);
}
// else if ((wc.isWallSliding ) && Input.GetAxis("Horizontal") > 0 && !isJumping)
// {
// wc.isWallSliding = false;
// extraJumps--;
// Vector2 forceToAdd = new Vector2(wjc.wallJumpForce * wjc.wallJumpDirection.x * Input.GetAxis("Horizontal"), wjc.wallJumpForce * wjc.wallJumpDirection.y);
//pw.rb.AddForce(forceToAdd, ForceMode2D.Impulse);
// }
if (Input.GetButtonUp("Jump"))
{
pw.rb.velocity = new Vector2(pw.rb.velocity.x, pw.rb.velocity.y * variableJumpHeight);
}
}
private void Jump()
{
if (!wc.isWallSliding && canJump)
{
pw.rb.velocity = new Vector2(pw.rb.velocity.x, jumpForce); ;
extraJumps--;
}
else if (wc.isWallSliding && Input.GetAxis("Horizontal") == 0 && canJump) //Wall hop
{
wc.isWallSliding = false;
extraJumps--;
Vector2 forceToAdd = new Vector2(wjc.wallHopForce * wjc.wallHopDirection.x * -wjc.facingDirection, wjc.wallHopForce * wjc.wallHopDirection.y);
pw.rb.AddForce(forceToAdd, ForceMode2D.Impulse);
}
else if ((wc.isWallSliding || wc.isTouchingWall) && Input.GetAxis("Horizontal") != 0 && canJump)
{
wc.isWallSliding = false;
extraJumps--;
Vector2 forceToAdd = new Vector2(wjc.wallJumpForce * wjc.wallJumpDirection.x * Input.GetAxis("Horizontal"), wjc.wallJumpForce * wjc.wallJumpDirection.y);
pw.rb.AddForce(forceToAdd, ForceMode2D.Impulse);
}
}
}
}
I would use 2d colliders to check if your touching a ground object. Then on your movement method I would check if you are touching it. If you aren't, then change movespeed.