- Home /
time counter in scripting
is there any way to set a timer in unity? I used Time.deltaTime:
jumpTimeCounter = jumpTimeCounter - Time.deltaTime;
In this function:
private void jump()
{
Debug.Log(Time.deltaTime);
if(canJump == true && Input.GetKeyDown(KeyCode.Space) == true)
{
playerRb.velocity = Vector2.up * jumpForce;
canJump = false;
}
if(Input.GetKey(KeyCode.Space) == true)
{
if(jumpTimeCounter > 0)
{
//mettere su anki speigazione di questo codice
playerRb.velocity = new Vector2(playerRb.velocity.x, 0) + Vector2.up * jumpForce;
jumpTimeCounter = jumpTimeCounter - Time.deltaTime;
}
}
if(Input.GetKeyUp(KeyCode.Space))
jumpTimeCounter = 0;
}
But the player occasionally jumps to different heights.
This is the output of Debug.Log (Time.deltaTime)
:
the whole class code is: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerControl : MonoBehaviour
{
private Rigidbody2D playerRb;
//variabili movimento orizontale
public float speedHorizontal;
private float horizontalInput;
//variabili salto
private bool canJump = false;
public float jumpForce;
public float jumpTimeCounter;
public float maxJumpTime;
private float defaultGravityScale;
public float moltiplicatoreGravityScale;
void Start()
{
playerRb = gameObject.GetComponent<Rigidbody2D>();
defaultGravityScale = playerRb.gravityScale;
moltiplicatoreGravityScale = moltiplicatoreGravityScale*playerRb.gravityScale;
jumpTimeCounter = maxJumpTime;
}
// Update is called once per frame
void Update()
{
horizontalMove();
jump();
}
void FixedUpdate()
{
if(playerRb.velocity.y < 0)
playerRb.gravityScale = moltiplicatoreGravityScale;
else
playerRb.gravityScale = defaultGravityScale;
}
private void horizontalMove()
{
float horizontalInput = Input.GetAxis("Horizontal");
playerRb.velocity = new Vector2(speedHorizontal * horizontalInput, playerRb.velocity.y);
}
private void jump()
{
Debug.Log(Time.deltaTime);
if(canJump == true && Input.GetKeyDown(KeyCode.Space) == true)
{
playerRb.velocity = Vector2.up * jumpForce;
canJump = false;
}
if(Input.GetKey(KeyCode.Space) == true)
{
if(jumpTimeCounter > 0)
{
//mettere su anki speigazione di questo codice
playerRb.velocity = new Vector2(playerRb.velocity.x, 0) + Vector2.up * jumpForce;
jumpTimeCounter = jumpTimeCounter - Time.deltaTime;
}
}
if(Input.GetKeyUp(KeyCode.Space))
jumpTimeCounter = 0;
}
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Ground")
{
canJump = true;
jumpTimeCounter = maxJumpTime;
}
}
}
Answer by HellsHand · Mar 31, 2021 at 06:23 PM
Have you tried putting your jump() function into FixedUpdate()?
yes, but then, sometimes, it doesn't take the space bar input (I also tried to take the input in Update () and pass it to FixedUpdate (), but the problem still occurred)
What about using a for loop to move your character up and then adjusting how long the loop lasts?
On second thought that might just make it worse.
Well this is strange, your code works perfect for me in fixedupdate(), jumpTimeCounter stops at -0.0199958 each time in 5 different jumps.
Answer by dylan1812 · Mar 31, 2021 at 09:43 PM
You should always listen for Inputs in Update and apply physics in FixedUpdate.
You could try something like:
void Update()
{
if (Input.GetKeyUp(KeyCode.Space)
{
shouldJump = true;
}
}
void FixedUpdate()
{
if (shouldJump)
{
shouldJump = false;
//do jump things
}
}
Your answer
Follow this Question
Related Questions
Player movement problem 0 Answers
How to prevent player from moving in air while jumping in place?? 0 Answers
how to make player jump from clicking a box collider? 3 Answers
Player cannot jump 0 Answers
Jump Issue 0 Answers