- Home /
How do I get my character to keep his left and right momentum in the air while also disabling movement controls until grounded?
Hello! I am a student currently working on a unity group project for school and I'm pretty new to coding. My friends and I are trying to make a very difficult 2d platformer and one of our ideas was to make it so that the character's air momentum stayed when jumping(or falling) to the sides while also disabling horizontal movement controls until landing on a platform again. The issue is that I haven't found anybody online that has a groundcheck or a script for this that doesn't require me having to rewrite the whole character controller. This is the movement script so far:
public class CharacterBehaviour : MonoBehaviour
{
public float MovementSpeed = 1;
public float JumpForce = 1;
private Rigidbody2D _rigidbody;
void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
}
}
Can I implement code for this into this already existing script or do I have to rewrite it all? And if I don't have to rewrite it, how do I do it? Thanks a bunch in advance!
Your answer
Follow this Question
Related Questions
how to jump at fixed height but faster 0 Answers
Bullet not moving from script 3 Answers
Gravity switch 1 Answer