- Home /
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:
Thanks in advance for any help!!
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.
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!
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.
Ideally, you're supposed to have all calls to Input in Update functions, and physics calls, including most calls to Rigidbody2D, in FixedUpdate functions.
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
Follow this Question
Related Questions
Make a 2D jump? 0 Answers
2D - Change jump direction 1 Answer
Creating A wall Jump 0 Answers