- Home /
Question by
baiatul_antisocial99 · Mar 16, 2021 at 08:15 AM ·
movementvelocityjump
player velocity increases while in air
If I keep pressing W or (ASD) after jumping my player's velocity increases a lot. (From 3 to about 7-8). I want to be able to move my character in air but keep the same speed or at least have just a little increase.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
public float _speed;
public float jumpForce;
private bool isGrounded;
public Vector3 jump;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
private void OnCollisionStay(Collision collision)
{
isGrounded = true;
}
private void OnCollisionExit(Collision collision)
{
isGrounded = false;
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
rb.AddForce(new Vector3(horizontalInput, 0, verticalInput) * _speed, ForceMode.Force);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613030751im_/https://answers.unity.com/themes/thub/images/avi.jpg)