Jump every 2 seconds (Character controller) By Mike Geig

Hi, I saw the live training of 2d Character controller by Mike Geig, and I’m trying that my character jump every 2 seconds, here’s the code, the only thing I want the character to do is always to be running and jump every seconds, I already did the first thing but I cant get it to jump, thanks for your help, :), here’s the code

using UnityEngine;
using System.Collections;

public class RobotControllerScripts : MonoBehaviour {

	public float maxSpeed = 10f;
		bool facingRight = true;

	Animator anim;

	bool grounded = false;
	public Transform groundCheck;
	float groundRadius = 0.2f;
	public LayerMask whatIsGround;
	public float jumpForce = 700f;

	bool doubleJump = false;
	void Start () 
	{
		anim = GetComponent<Animator> ();
	}

	void FixedUpdate () 
	{

		grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
		anim.SetBool ("Ground", grounded);

		if (grounded)
			doubleJump = false;

		anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);


		if (!grounded) return;
		float move = 1;

		anim.SetFloat ("Speed", Mathf.Abs (move));

		rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);

		if (move > 0 && !facingRight)
						Flip ();
		else if (move < 0 && facingRight)
						Flip ();
	}

	void Update()
	{
		if ((grounded || !doubleJump) && Input.GetKeyDown (KeyCode.Space)) 
		{
			anim.SetBool("Ground", false);
			rigidbody2D.AddForce (new Vector2(0, jumpForce));

			if(!doubleJump && !grounded)
				doubleJump = true;
		}
	}

	void Flip()
	{
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}
}

Also, since I only want the character to always be running and jump every x seconds, the character will not receive input from the player, so if there was a better way to do it, thanks for your help, you’re an awesome community :smiley:

I would make a function called Jump(), or something of the like, and in your update function have an InvokeRepeating() function that adds an upward force.

void Start()
{
    InvokeRepeating("Jump", 0f, 2f);
}

void Jump()
{
    anim.SetBool("Ground", false);
    rigidbody2D.AddForce (new Vector2(0, jumpForce));
}