Question by
razzaqzharfan · Nov 20, 2015 at 09:58 PM ·
c#charactercontroller
My Character won't jump
I was following a 2D platformer tutorial on youtube and soon after changing a few settings for the game object, my character won't jump. I have no experience in C# so any help is greatly appreciated. Thank you in advance. Attached is the code I used from following the tutorial.
using UnityEngine; using System.Collections;
public class Movement : MonoBehaviour{
public float moveSpeed;
public float jumpHeight;
private float moveVelocity;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
private Rigidbody2D myRigidBody2D;
// Use this for initialization
void Start(){
myRigidBody2D = GetComponent <Rigidbody2D>();
}
void FixedUpdate(){
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius,whatIsGround);
}
// Update is called once per frame
void Update(){
if (grounded)
{
doubleJumped = false;
}
if (Input.GetKeyDown(KeyCode.Space) && grounded )
{
//myRigidBody2D.velocity = new Vector2(myRigidBody2D.velocity.x,jumpHeight);
Jump();
}
if (Input.GetKeyDown(KeyCode.Space) && !doubleJumped && grounded)
{
//myRigidBody2D.velocity = new Vector2(myRigidBody2D.velocity.x, jumpHeight);
Jump();
doubleJumped = true;
}
moveVelocity = 0f;
if (Input.GetKey(KeyCode.D))
{
//myRigidBody2D.velocity = new Vector2(moveSpeed,myRigidBody2D.velocity.y);
moveVelocity = moveSpeed;
}
if (Input.GetKey(KeyCode.A))
{
//myRigidBody2D.velocity = new Vector2(-moveSpeed,myRigidBody2D.velocity.y);
moveVelocity = -moveSpeed;
}
myRigidBody2D.velocity = new Vector2(moveVelocity, myRigidBody2D.velocity.y);
}
public void Jump(){
myRigidBody2D.velocity = new Vector2(myRigidBody2D.velocity.x, jumpHeight);
}
}
Comment