I am following a book on making a 2D platformer game and the script they walked me though did not work. I have reworked it so my player can now move left and right and his animation flips correctly but I still need to add a jump feature. I have tried several and none seem to work. This is the code that is currently working:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float maxSpeed = 10f;
bool facingRight = true;
void Start ()
{}
void FixedUpdate()
{
float move = Input.GetAxis(“Horizontal”);
GetComponent().velocity = new Vector2(move * maxSpeed, GetComponent().velocity.y);
if (move > 0.0f && !facingRight)
Flip ();
else if (move < 0.0f && facingRight == true)
Flip();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
But when I try and modify it to allow for jumping it wont work. I assumed the quick solution was to just add a single line to activate the JUMP features in the input manager like this:
float jump = Input.GetAxis(“Jump”);
But that doesnt work. Why does the horizontal work but the jump doesnt?
I understand I am not using the ground check or referencing any animators at this time but the author of the book felt that would come in the next chapter.
please use [code ][/code ] tags when pasting code into the forums, it adds line numbers and helps with readability. There is a sticky on them at the top of the scripting forum.
The Input string names “Horizontal” “Vertical” “Jump” etc. are setup in the input manager, I’m not sure that “jump” is in the default setup:
generally for a jump you will not be using an Axis (continuous input in a “direction”) you’ll be using a button (press “space” to jump etc.), so you will need to look at Input.GetKey#/GetButton# functions
given that you are using a rigidbody you’ll likely end up using AddForce to send the character skywards.
common pitfall is not using the right ForceMode in the second argument, if you want to instantly apply a force it’s “impulse” not “force”
Between your post and some Google-Fu, I was able to solve my issue. Here is what my code now looks like and my player can run and jump great.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
[HideInInspector]
public bool isFacingRight = true;
[HideInInspector]
public bool isJumping = false;
[HideInInspector]
public bool isGrounded = false;
public float maxSpeed = 7.0f;
public float jumpForce = 650.0f;
public Transform groundCheck;
public LayerMask groundLayers;
private float groundCheckRadius = 0.2f;
void Start ()
{}
void Update ()
{
if (Input.GetButtonDown ("Jump"))
{
if (isGrounded == true)
{
this.GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, 0);
this.GetComponent<Rigidbody2D>().AddForce (new Vector2 (0, jumpForce));
}
}
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle
(groundCheck.position, groundCheckRadius, groundLayers);
float move = Input.GetAxis ("Horizontal");
this.GetComponent<Rigidbody2D>().velocity =
new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if ((move > 0.0f && isFacingRight == false) || (move < 0.0f && isFacingRight == true))
{
Flip ();
}
}
void Flip ()
{
isFacingRight = !isFacingRight;
Vector3 playerScale = transform.localScale;
playerScale.x = playerScale.x * -1;
transform.localScale = playerScale;
}
}