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 /
  • Help Room /
avatar image
0
Question by Bilal_Aljandly · Oct 05, 2021 at 08:25 AM · movementtimejumpif-statementsloops

how can i repeat an if statment for x sec?

Hi! I trying to make the Player jump if it is grounded and if the player isn't grounded check if the Player will be grounded in the next x sec. So if the Player become grounded i the x sec it will jump.

The First Part is working will but the checking for x sec part I don't know how to do it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movements : MonoBehaviour
 {
 
     [SerializeField] Rigidbody2D rg;
     [SerializeField] float movementSpeed;
     [SerializeField] float maxSpeed;
     [SerializeField] float jumpForce;
     [SerializeField] float raydis;
     [SerializeField] LayerMask GroundLayerMask;
 
     public float jumpCheckingTime = 0.3f;
 
     // Start is called before the first frame update
     void Start()
     {
         rg = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (Input.GetKeyDown(KeyCode.Space))
             Jump(); 
     }
 
     private void FixedUpdate()
     {
 
         float moveX = Input.GetAxis("Horizontal");
 
         if(Input.GetKey(KeyCode.A) && rg.velocity.x > -maxSpeed)
             rg.AddForce(new Vector2(moveX * movementSpeed, 0f), ForceMode2D.Force);
         if(Input.GetKey(KeyCode.D) && rg.velocity.x < maxSpeed)
             rg.AddForce(new Vector2(moveX * movementSpeed, 0f), ForceMode2D.Force);
 
 
     }
 
     bool IsGrounded()
     {
         RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, raydis, GroundLayerMask);
         if (hit.collider != null)
         {
             if (hit.collider.tag == "Ground")
             {
                 return true;
             }
             else return false;
         }
         else
         {
             return false;
         }
 
     }
 
     void Jump()
     {
 
         if (IsGrounded())
         {
             rg.velocity = new Vector2(rg.velocity.x, 0f);
             rg.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
         }
         else
         {
             // Check for x Sec is the player will be grouned
         }
     }
 
 }
 

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

1 Reply

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

Answer by Hellium · Oct 05, 2021 at 08:56 AM

CODE NOT TESTED

  [SerializeField] float jumpRequestBufferDuration = 0.2f;
  float lastJumpRequestTime = Mathf.NegativeInfinity;
  void Update()
  { 
      if (Input.GetKeyDown(KeyCode.Space) || Time.timeSinceLevelLoad < lastJumpRequestTime + jumpRequestBufferDuration)
          Jump(); 
  }
 
  void Jump()
  { 
      if (IsGrounded())
      {
          rg.velocity = new Vector2(rg.velocity.x, 0f);
          rg.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
          lastJumpRequestTime = Mathf.NegativeInfinity;
      }
      else
      {
          lastJumpRequestTime = Time.timeSinceLevelLoad;
      }
  }
Comment
Add comment · Show 2 · 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 Bilal_Aljandly · Oct 05, 2021 at 09:28 AM 0
Share

thanks, it did work. I will be grateful if you can explain how it is working

avatar image Hellium Bilal_Aljandly · Oct 05, 2021 at 09:44 AM 1
Share

The code is pretty simple.

When jump is requested, if the character is not grounded, I save the time.

In Update, I check whether the request time was less than jumpRequestBufferDuration seconds ago (i.e if the current time is less than the request time + buffer duration)

  if(Time.timeSinceLevelLoad < lastJumpRequestTime + jumpRequestBufferDuration)

is the same as

 if(lastJumpRequestTime > Time.timeSinceLevelLoad - jumpRequestBufferDuration

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

230 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 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

Character keeps flying after jumping 0 Answers

Get Time A Bool Has Been True 4 Answers

How to speed up enemy after time? 1 Answer

Play animation when WASD is pressed, stop animation when none of them are pressed (Help!!) 0 Answers

I have a problem with in air movement 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