Unity Rigidbody fps controller jumps higher each time.
Hi i am trying to make a first person controller using the rigidbody. I found an article outlining how accelartation works in Counterstrike and how bhopping is implimented through that. I wanted to integrate that into my code. It all works fine, however I have a queue jump bool that will allow you to jump immediately if you hold space while in the air. for some reason if you do this the character will jump higher each time. I cant seem to figure it out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody)),RequireComponent(typeof(CapsuleCollider))]
public class PlayerMovement : MonoBehaviour
{
public float max_velocity_ground;
public float ground_accelerate;
public float air_accelerate;
public float max_velocity_air;
public float friction;
public Vector3 gravity = new Vector3(0, -4, 0);
public Vector3 jumpHeight;
Ray groundCheck;
Rigidbody rbody;
Collider col;
RaycastHit hit;
bool isGrounded = false;
bool queueJump = false;
float inputY, inputX;
void Start ()
{
rbody = GetComponent<Rigidbody>();
col = GetComponent<CapsuleCollider>();
}
void Update ()
{
inputY = Input.GetAxis("Vertical");
inputX = Input.GetAxis("Horizontal");
//Accel direction is normalized wishDir
groundCheck = new Ray(transform.position, -Vector3.up);
Vector3 wishDir = transform.forward * inputY + transform.right * inputX;
wishDir = Vector3.ProjectOnPlane(wishDir, Vector3.down).normalized;
if(!isGrounded)
{
if(Input.GetButtonDown("Jump"))
{
queueJump = true;
}
if(Input.GetButtonUp("Jump"))
{
queueJump = false;
}
}
if (Input.GetButtonDown("Jump") && isGrounded || queueJump && isGrounded)
{
//rbody.AddForce(jumpHeight);
rbody.velocity += jumpHeight;
}
if (Physics.Raycast(groundCheck, out hit, 15.0f))
{
isGrounded = true;
rbody.velocity = MoveGround(wishDir, rbody.velocity);
Debug.Log(rbody.velocity);
}
else
{
isGrounded = false;
rbody.velocity = MoveAir(wishDir, rbody.velocity);
rbody.velocity += gravity;
}
//Debug.DrawRay(groundCheck.origin, groundCheck.direction * 15.0f, Color.red);
}
private Vector3 Accelerate(Vector3 accelDir, Vector3 prevVelocity, float accelerate, float max_velocity)
{
float projVel = Vector3.Dot(prevVelocity, accelDir); // Vector projection of Current velocity onto accelDir.
float accelVel = accelerate * Time.fixedDeltaTime; // Accelerated velocity in direction of movment
if (projVel + accelVel > max_velocity)
accelVel = max_velocity - projVel;
return prevVelocity + accelDir * accelVel;
}
private Vector3 MoveGround(Vector3 accelDir, Vector3 prevVelocity)
{
// Apply Friction
float speed = prevVelocity.magnitude;
if (speed != 0) // To avoid divide by zero errors
{
float drop = speed * friction * Time.fixedDeltaTime;
prevVelocity *= Mathf.Max(speed - drop, 0) / speed; // Scale the velocity based on friction.
}
return Accelerate(accelDir, prevVelocity, ground_accelerate, max_velocity_ground);
}
private Vector3 MoveAir(Vector3 accelDir, Vector3 prevVelocity)
{
return Accelerate(accelDir, prevVelocity, air_accelerate, max_velocity_air);
}
}
NOTE: everything below the update I copied from the article and was trying to incorporate it.
Comment