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 mrlchristie · May 03, 2014 at 02:35 PM · 2djumpjumpingplatformer

Advice needed on my first c# 2D jump script

I'm new to Unity and scripting in general. I am currently learning the basics and have created a simple character move script that includes jumping.

In Unity I have just drawn a couple of cube game objects to act as a platform with 2D colliders attached.

My 'character' is just another 2D cube that can travel left and right across the platform's and jump by detecting the ground with a Linecast.

My problem is this: When moving the character cube along the platform and pressing the jump key I have assigned, it sometimes doesn't jump as if it doesn't realise I've pressed the jump key. It's almost like it has to delay the next jump?

Here is my code (comments for my own learning):

 sing UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour { //this script is attached to the player game object
 
     //player
     public float speed; //declare the player move speed in Unity inspector
     public int jumpHeight; //this is set in the Unity Inspector
     public bool isGrounded = false; //this can be seen working in the Unity inspector
     public Transform groundedEnd; //declares the empty game object in Unity acting as a collider set to the position of the player
 
 
     void Start () {
     }
 
     void FixedUpdate () 
     {
         Movement (); //call the movement function below
         isGrounded = Physics2D.Linecast(this.transform.position, groundedEnd.position, 1 << LayerMask.NameToLayer("Ground")); 
         //the above line of code draws a linecast downwards to detect the ground game objects that have been placed in a ground layer
     }
 
     void Movement()
     {
             //Move Right
             if (Input.GetKey (KeyCode.D)) 
             {
                 transform.Translate (Vector2.right * speed * Time.deltaTime);
                 transform.eulerAngles = new Vector2(0,0); 
             }
             //Move Left
             if (Input.GetKey (KeyCode.A)) 
             {
                 transform.Translate (Vector2.right * speed * Time.deltaTime);
                 transform.eulerAngles = new Vector2(0,180); //flip the character on its x axis
             }
             //Jump by detecting if the user presses W and then checking to see if the linecast is touching the ground
             if (Input.GetKeyDown (KeyCode.W) && isGrounded == true)
             {
             //Add force to the players rigidbody allowing it to move upwards if the above if statement is true
             rigidbody2D.AddForce (Vector2.up * jumpHeight);
             }
             
     }
     
 }

Here is the Unity view:

alt text

Thanks in advance for any help!!

platformer.jpg (308.3 kB)
Comment
Add comment · Show 2
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 AtLeastImTrying · Apr 23, 2017 at 03:42 AM 0
Share

I would like to say that flipping your character on the x axis might be useless if your character is a square, but I'm not sure if you were going to change the sprites at some point.

avatar image toddisarockstar · Apr 23, 2017 at 05:05 AM 0
Share

i highly recommend OnCollisionStay to detect if your player is grounded. It's easier on the CPU and more accurate if you are walking on angles surfaces!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by mrlchristie · May 08, 2014 at 06:26 PM

Putting the jump part of my code in an Update function seemed to sort it out.

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 Pyrian · May 08, 2014 at 06:59 PM 1
Share

Ideally, you're supposed to have all calls to Input in Update functions, and physics calls, including most calls to Rigidbody2D, in FixedUpdate functions.

avatar image mrlchristie · May 09, 2014 at 11:16 AM 0
Share

Cheers, Pyrian. I've also just watched the 2D character controller tutorial on the Learn section and it's also mentioned on that.

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

23 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

Related Questions

Make a 2D jump? 0 Answers

2D - Change jump direction 1 Answer

Creating A wall Jump 0 Answers

2D platformer jumping that feels good 1 Answer

This script doesn't make smooth jump why ? 2 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