Hi Everyone,
Im playing around with a simple 2d platformer.
Ive created a player who can walk around and jump whilst using the animation component to add some simple animations based on his state, walking, jumping etc.
I found my code to be working fine, player walks jumps. animations all play when they should.
Ive run into an odd issue though when i added a second, higher platform, for the player to jump to.
The player will jump, the jump animation will play but when the player lands on the second platform he gets stuck in the jump animation, as if it hasnt detected the ground.
The player will only return to āno jumpingā mode when he is moved back to the original lower platform her jumped from.
Same happens if i start the player off on the higher platform, he can jump and move fine until he lands on the lower platform where suddenly hes stuck in jump mode.
Anyone have any ideas? I was thinking maybe it was something to do with the linecasting looking for the original Y coordinates in world space.?
Both platforms are on the Ground layer also.
Any help would be great, please ask if you need me to clarify anything.
public class PlayerCont : MonoBehaviour {
float h ;
Rigidbody2D rb2d;
bool facingRight = true;
float speed = 5;
float jumpPower = 300f;
Animator anim;
bool jumping = false;
private Transform groundCheck;
bool grounded = false;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator> ();
groundCheck = transform.Find ("groundCheck");
}
void Update()
{
//print (groundCheck.position);
grounded = Physics2D.Linecast(transform.position,groundCheck.position ,1 << LayerMask.NameToLayer("Ground"));
//print (groundCheck.position);
}
// Update is called once per frame
void FixedUpdate () {
h = Input.GetAxisRaw ("Horizontal");
//print (h);
//Move player
//Moving right
if (h == 1) {
float movespeedR = 1 * speed;
rb2d.velocity = new Vector2 (movespeedR ,rb2d.velocity.y ) ;
anim.SetBool("Walks",true);
}
//Moving left
if (h == -1) {
float movespeedL = -1 * speed;
rb2d.velocity = new Vector2(movespeedL ,rb2d.velocity.y);
anim.SetBool ("Walks", true);
}
//Stopped moving
if (h == 0 && jumping == false) {
rb2d.velocity = new Vector2 (0, 0);
anim.SetBool ("Walks", false);
}
//Player Jump
if (Input.GetKeyDown(KeyCode.Space) && jumping == false)
{ jumping = true;
rb2d.AddForce (Vector2.up * jumpPower);
anim.SetBool ("Jump", true);
}
if (jumping == true && grounded == true) {
jumping = false;
anim.SetBool ("Jump", false);
}
}
}