Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shyvert9 · Mar 31, 2021 at 05:43 PM · playerjumptime.deltatimetimer countdown

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):

alt text

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;
         }
     }
 }
 
cattura.png (48.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HellsHand · Mar 31, 2021 at 06:23 PM

Have you tried putting your jump() function into FixedUpdate()?

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shyvert9 · Mar 31, 2021 at 06:28 PM 0
Share

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)

avatar image HellsHand shyvert9 · Mar 31, 2021 at 06:38 PM 0
Share

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.

avatar image shyvert9 HellsHand · Mar 31, 2021 at 07:45 PM 0
Share

Was the spacebar input always being taken?

Show more comments
avatar image
0

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
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

147 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges