• 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 arturoza · Aug 16, 2014 at 03:21 AM · charactercontrollerjump

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

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
0
Best Answer

Answer by nintorii · Aug 16, 2014 at 06:56 AM

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));
 }


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 arturoza · Aug 17, 2014 at 01:05 AM 0
Share

Thanks, It worked but I have to put the InvokeRepeating in the Start Function!

avatar image nintorii · Aug 17, 2014 at 01:22 AM 0
Share

$$anonymous$$y bad! I wrote the code without testing it, so sorry for the confusion. Edited for any future users who see this.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Gravity and Jumping for a Character Controller 1 Answer

Let Character Controller jump. 0 Answers

Let Character Controller jump. 1 Answer

How to fix a infinity jump? 1 Answer

C# Jumping Issue 1 Answer


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