I want the player object to jump and to be able to control it while in the air and that all works but it feels I can move too fast left or right, while jumping wich leads to covering a lot of area.
I want the jump to smoother so when I jump, I move left or right at a reduced speed and fall down in a shorter distance.
Here is my code:
using System;
using NUnit.Framework;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundCheckRadius = 0.2f;
[SerializeField] private LayerMask groundLayer;
public ContactFilter2D ContactFilter;
private float _forwardInput;
// jumping logic
private float _sideSpeed = 6f;
private float _mSideSpeed;
public float jumpVelocity = 6f; // Initial upward velocity when jumping
private bool _isJumping; // true when the hero is in the air
private float _verticalVelocity; // current vertical velocity
private bool _isInTheAir;
public Rigidbody2D playerBody;
public bool IsGrounded => playerBody.IsTouching(ContactFilter);
private void Start()
{
playerBody = GetComponent<Rigidbody2D>();
playerBody.freezeRotation = true;
}
// Update is called once per frame
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
_isJumping = true;
}
_mSideSpeed = (Input.GetKey(KeyCode.LeftArrow) ? -_sideSpeed : 0f) + (Input.GetKey(KeyCode.RightArrow) ? _sideSpeed : 0f);
}
private void FixedUpdate()
{
Vector2 currentVelocity = playerBody.linearVelocity;
if (_isJumping && IsGrounded)
{
currentVelocity.y = jumpVelocity;
}
playerBody.linearVelocity = new Vector2(_mSideSpeed, playerBody.linearVelocity.y);
currentVelocity.x = _mSideSpeed;
playerBody.linearVelocity = currentVelocity;
_isJumping = false;
_mSideSpeed = 0f;
}
}
Also here is a video of the issue:
I tried reducing the speed while in the air but that causes the object to fall down slowert and thats the furthest I got. Any ideas? thanks in advance!
You would need separate values for movement on ground or in air.
This could be in the form of either:
different speed limits
different control effectiveness
possibly different damping when in air.
If any part of your velocity is impacted by the Rigidbody dragging on the ground, then you would need to factor in the PhysicsMaterial2D frictional properties because obviously those would NOT be affecting the character when in air.
And you want to get away from gigantic hairy lines of unreadable code like this one:
If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.
Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.
The longer your lines of code are, the harder they will be for you to understand them.
Thanks for the above and yes, the code can def be better.
I did not consider the friction on the ground compared to the object being in the air, that is good to know.
Is the jump buffer the prefered way to do jump logic?
No prob, I only point it out because code like that becomes a blocking point for extending and improving your code, because to do anything meaningful you have to first decompose it, then recompose it.
That’s why most of my code is one (maybe two) things per line, line after line going down, but some people just don’t like that verticality. I love it.
Players like it because it feels like they got something for free.