- Home /
RB velocity instantly changes/ overwrites when adding a new input .C#
I am using rigidbody.addRelativeForce to move the player and the player also has a look script attached to it. I want to limit the horizontal movement while the player is in the air, but still fly through the air with the initial velocity that the player was moving in before the jump/fall. The issue is after jumping if there is any movement input while in the air, the player will ignore the initial velocity it was moving in before the jump. I increased the velocity that's taken from the movement before the jump and added it to the jump velocity. The velocity gets added in the first few frames, but then gets ignored or canceled out and only moves with the AirSpeed variable that's set to 3.
Current behavior
Expected behavior
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Movement
[Header("Movement")]
public float speedometer;
[SerializeField, Range(0f, 40f)] float GroundSpeed = 10, GroundAccelX = 10, GroundAccelZ;
[SerializeField, Range(0f, 20f)] float AirSpeed = 3, AirAccelZ = 3, AirAccelX;
[SerializeField, Range(0f, 1f)] float counterMovementX, counterMovementZ;
[SerializeField, Range(0f, 1f)] float AirDragX, AirDragZ;
// float maxSpeed;
public Vector3 velocity, desiredVelocity, velocityChange;
public float rayHitdistance = 1;
public LayerMask whatIsGround;
//Jump
[Header("Jump")]
[SerializeField, Range(0f, 20f)] float jumpHight = 5;
bool grounded;
// Inputs
float inputX, inputZ;
bool inputJump;
Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void Start()
{
// maxSpeed = GroundSpeed;
}
void Inputs()
{
inputX = Input.GetAxisRaw("Horizontal");
inputZ = Input.GetAxisRaw("Vertical");
inputJump |= Input.GetButtonDown("Jump");
}
void Update()
{
Inputs();
speedometer = rb.velocity.magnitude;
}
void FixedUpdate()
{
if (Mathf.Abs(inputX) > 0 || Mathf.Abs(inputZ) > 0)
{
MoveVelocity();
}
else if (OnGround())
{
CounterMovement(counterMovementX, counterMovementZ);
}
if (inputJump && OnGround())
{
Jump();
inputJump = false;
}
UpdateState();
}
// used for reseting the volicty after jumping
void UpdateState()
{
if (rb.velocity.y < -0.1f)
{
if (OnGround())
{
velocity = Vector3.zero;
}
}
}
void Jump()
{
float mag = rb.velocity.magnitude;
float jumpForce = Mathf.Sqrt(-2f * Physics.gravity.y * jumpHight);
Vector3 vel = transform.InverseTransformDirection(rb.velocity); // velocity drag gets converted from word space to local space
Vector3 desiredVel = vel * mag;
rb.AddRelativeForce(3* vel + Vector3.up * jumpForce, ForceMode.Impulse);
}
void CounterMovement(float xCounter, float zCounter)
{
Vector3 vel = transform.InverseTransformDirection(rb.velocity); // velocity drag gets converted from word space to local space
vel.x *= xCounter;
vel.z *= zCounter;
rb.velocity = transform.TransformDirection(vel); // velocity drag gets converted from local space to world space
}
void MoveVelocity()
{
float Movespeed = OnGround() ? GroundSpeed : AirSpeed;
desiredVelocity = new Vector3(inputX, 0f, inputZ).normalized * Movespeed;
desiredVelocity = transform.TransformDirection(desiredVelocity);
velocity = rb.velocity;
velocityChange = (desiredVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -Movespeed, Movespeed);
velocityChange.z = Mathf.Clamp(velocityChange.z, -Movespeed, Movespeed);
velocityChange.y = 0;
rb.AddForce(velocityChange, ForceMode.VelocityChange);
}
bool OnGround()
{
if (Physics.Raycast(transform.position, Vector3.down, rayHitdistance, whatIsGround) ||
Physics.Raycast(transform.position - new Vector3(0.5f, 0, 0), Vector3.down, rayHitdistance, whatIsGround) ||
Physics.Raycast(transform.position - new Vector3(-0.5f, 0, 0), Vector3.down, rayHitdistance, whatIsGround) ||
Physics.Raycast(transform.position - new Vector3(0, 0, 0.5f), Vector3.down, rayHitdistance, whatIsGround) ||
Physics.Raycast(transform.position - new Vector3(0, 0, -0.5f), Vector3.down, rayHitdistance, whatIsGround))
{
return true;
}
else
{
return false;
}
}
}
Your answer
Follow this Question
Related Questions
Simple rigidbody.AddForce only applying once each time key is pressed 1 Answer
Stop movement of rigid bodies 2D after collision 1 Answer
How to make RigidBody's movement using fixed movement/velocity and other forces all together? 3 Answers
Addforce for shotgun raycasts 0 Answers
Character flies away at high speed with Rigidbody.AddForce 1 Answer