Jumping


Joined
Mar 12, 2017
Messages
1
Reaction score
0
I am creating a 2d platformer game and my character cannot jump even though i checked over it a million times. I am really new to coding c# and know not much but i am using unity 5.5 and cannot figure this out. here is my code...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playercontroller : MonoBehaviour {

//movement variables
public float maxSpeed;

//jumping variables
bool grounded = false;
float groundCheckRadius = 0.5f;
public LayerMask groundLayer;
public Transform groundCheck;
public float jumpHeight;

Rigidbody2D myRB;
Animator myAnim;
bool facingRight;

// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();

facingRight = true;

}

// Update is called once per frame
void update()
{
if(grounded && Input.GetAxis("Jump") > 0)
{
grounded = false;
myAnim.SetBool("isGrounded",grounded);
myRB.AddForce(new Vector2(0, jumpHeight));
}
}


void FixedUpdate () {

//check if we are grounded - if no, then we are falling
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
myAnim.SetBool("isGrounded", grounded);

myAnim.SetFloat("verticalSpeed", myRB.velocity.y);


float move = Input.GetAxis("Horizontal");
myAnim.SetFloat("speed", Mathf.Abs(move));

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

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

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

Attachments

  • upload_2017-3-12_4-54-37.png
    upload_2017-3-12_4-54-37.png
    382.7 KB · Views: 287
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top