Frame Base Jump Loop
I have a problem with my frame based jumping loop. It does the check every frame to see if the vertical axis is greater than 33%. Then it checks if the axis is being held down. It then executes the Jumping function which is supposed to keep the frame on which the jump is activate and then see is the current frame is equal to the stored frame plus 4. If that is the case then add to the vertical velocity variable which is later added in LateUpdate().
My problem is, is that check[1] is true on every frame so it sets my current frame to the newest one every time so the player never jumps. What I need is for check[1] to be done once so that it will store the correct frame and then jump 4 frames later.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WalkingController : Controller {
Vector3 walkVel;
float adjVertVel;
int jumpPressFrm;
float jumpVel = 0f;
// 0 is for isDashing(), 1 is for checkJumping,
int[] curFrm = new int[64];
bool[] check = new bool[64];
public override void ReadInput(InputData data)
{
walkVel = Vector3.zero;
adjVertVel = 0f;
//Debug.Log("Frame: " + Time.frameCount);
checkDashing(data);
checkJumpCrouch(data);
}
void LateUpdate()
{
rb.velocity = new Vector3(walkVel.x, rb.velocity.y + adjVertVel, walkVel.z);
walkVel = Vector3.zero;
}
void checkDashing(InputData data)
{
if (data.axes[1] != 0f && isDashing(data))
{
walkVel += Vector3.right * data.axes[1] * 3;
}
else if (data.axes[1] != 0f && !isDashing(data))
{
walkVel += Vector3.right * data.axes[1];
}
if (!isDashing(data) && data.axes[1] == 0f)
{
check[0] = true;
}
}
bool isDashing(InputData data)
{
while(check[0])
{
curFrm[0] = Time.frameCount;
check[0] = false;
}
if (Time.frameCount >= curFrm[0] + 12 || data.axes[1] == 0f)
{
return false;
}
return true;
}
//JumpCrouch is the state of the player before they enter jump
void checkJumpCrouch(InputData data)
{
if (data.axes[0] >= (1f / 3f) || data.buttons[2] == true)
{
if (jumpPressFrm == 0)
{
check[1] = true;
checkJumping(data);
}
jumpPressFrm += Time.frameCount;
}
else { jumpPressFrm = 0;}
}
//My problem is, is that check[1] is true on every frame so it sets my current frame to the
//newest one every time so the player never jumps. What I need is for check to be done once
//so that it will store the correct frame and then jump 4 frames later.
void checkJumping(InputData data)
{
while (check[1])
{
curFrm[1] = Time.frameCount;
jumpVel = 10f;
check[1] = false;
}
if (Time.frameCount >= curFrm[1] + 4 && Grounded())
{
adjVertVel += jumpVel;
jumpVel = 0f;
}
}
bool Grounded()
{
return Physics.Raycast
(transform.position, Vector3.down, coll.bounds.extents.y + 1f);
}
}
Your answer
Follow this Question
Related Questions
Breaking out of the right loop 1 Answer
Loop in coroutine stops after going halfway 2 Answers
Round Loop Problem?... 1 Answer
3D Side-Scrolling shooter looping background 0 Answers
How do I delete objects in an animation in a certain order? 1 Answer