How can I delay 1 second this code?
Hey guys, I've been trying to delay this code below to execute only after 1 second, no luck:
I'd like it to jump only after 1 second.
I don't know even the basics but I'm trying to learn. Sorry for that.
Regards.
void Update () {
grounded = Physics2D.IsTouchingLayers (myCollider, whatIsGround);
myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);
if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0))
{
if (grounded) {
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
}
}
}
Answer by $$anonymous$$ · Feb 21, 2017 at 03:04 AM
As far as I know, the only nice way to delay a piece of code in Unity is to use a Coroutine (or an IEnumerator rather). There are many ways to go about doing this, but I would do something like this:
IEnumerator CoUpdate (){
grounded = Physics2D.IsTouchingLayers (myCollider, whatIsGround);
myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);
if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0))
{
if (grounded) {
// Tells Unity to wait for 1 second
yield return new WaitForSeconds(1);
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
}
}
// Very important, this tells Unity to move onto next frame. Everything crashes without this
yield return null;
}
And then inside of your Start() or Awake() method include this line of code:
StartCoroutine(CoUpdate());
If your interested, I would read this. It talks about the basics of how Coroutines work. There lots of cool things you can do with it though like WaitUntil or WaitForSecondsRealtime. You can even create your own, but it's not necessary for this.
Apologies for any syntax errors, I'm just writing it here so it's possible I've made a mistake.
I hope this helped!
Answer by crawler167 · Feb 21, 2017 at 06:29 PM
Loved your help, i'm really trying to learn.
my whole code is like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController2 : MonoBehaviour {
public float moveSpeed;
public float jumpForce;
private Rigidbody2D myRigidbody;
public bool grounded;
public LayerMask whatIsGround;
private Collider2D myCollider;
public GameManager theGameManager;
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody2D> ();
myCollider = GetComponent<Collider2D> ();
StartCoroutine (CoUpdate());
}
// Update is called once per frame
void Update () {
IEnumerator CoUpdate()
{
grounded = Physics2D.IsTouchingLayers (myCollider, whatIsGround);
myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);
if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0))
{
if (grounded) {
//Tells Unity to wait 1 second
yield return new WaitForSeconds(1);
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
}
}
yield return null;
}
void OnCollisionEnter2D (Collision2D other)
{
if (other.gameObject.tag == "killbox")
{
theGameManager.RestartGame ();
}
if (other.gameObject.tag == "enemy")
{
myRigidbody.AddForce (new Vector3 (-7000f, 100f, transform.position.z));
}
}
}
My only problem is that console is giving me these errors:
Already tried to fix but no luck,
Regards.
Sorry. What I meant is to replace the void Update() with the IEnumerator CoUpdate. Both of these things are methods, but the IEnumerator returns a value (a delay). It should look something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController2 : $$anonymous$$onoBehaviour {
public float moveSpeed;
public float jumpForce;
private Rigidbody2D myRigidbody;
public bool grounded;
public Layer$$anonymous$$ask whatIsGround;
private Collider2D myCollider;
public Game$$anonymous$$anager theGame$$anonymous$$anager;
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody2D> ();
myCollider = GetComponent<Collider2D> ();
StartCoroutine (CoUpdate());
}
IEnumerator CoUpdate()
{
while(true){
grounded = Physics2D.IsTouchingLayers (myCollider, whatIsGround);
myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown (0))
{
if (grounded) {
//Tells Unity to wait 1 second
yield return new WaitForSeconds(1);
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
}
}
yield return null;
}
}
void OnCollisionEnter2D (Collision2D other)
{
if (other.gameObject.tag == "killbox")
{
theGame$$anonymous$$anager.RestartGame ();
}
if (other.gameObject.tag == "enemy")
{
myRigidbody.AddForce (new Vector3 (-7000f, 100f, transform.position.z));
}
}
}
sorry for the confusion
It doesn't give any errors. Seems ok, but when I press play the characters that has this script on just stands still, doing nothing.
Thank you for helping out anyway, if you know what is happening I'd love to know.
Regards.
The error you were getting is that there is a method inside of a method (it's the debugger's weird way of telling you that)
Answer by xdarkasgaming · Jun 24, 2020 at 08:30 PM
can you help me about my code, it didn't work for me. All i want is when game object (player) is destroyed, my gameover panel will appear and totally paused the entire screen. actually it works the waitforseconds after the player die the gameover panel pop out, but the game itself continue running.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerGameOver : MonoBehaviour {
public GameObject gameOverPanel, explosion, pauseButton, player;
public void Awake()
{
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag.Equals("Enemy"))
{
if (Time.timeScale == 1f)
Time.timeScale = 1f;
else
Time.timeScale = 0f;
StartCoroutine(waitForGO());
Instantiate(explosion, transform.position, Quaternion.identity);
gameOverPanel.SetActive(false);
FindObjectOfType<AudioManager1>().Play("PlayerDeath");
}
StartCoroutine(waitForGO());
}
IEnumerator waitForGO()
{
yield return new WaitForSeconds(3);
player.SetActive(false);
gameOverPanel.SetActive(true);
pauseButton.SetActive(false);
Time.timeScale = 0f;
}
}
Your answer
Follow this Question
Related Questions
Delay in setting Boolean Value 1 Answer
prefab time delay 1 Answer
How to wait a certain amount of seconds in C# 4 Answers
For Loop does two loops at once! 0 Answers
need help for adding a delay code before walking of npc 2 Answers