Hello, I am new to Unity and I just started trying to create a platform 2D and I have a small problem. My character falls rolling around and get down always falling, without being on feet. If it gets too fast or hits something it starts rolling. I would like it not to roll and to fall straight down, without turning around and things like that, even because if it isn’t on feet, my groundCheck doesn’t work and I can’t jump anymore.
Here’s the code :
public class PlayerController : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
public Transform GroundCheck;
public LayerMask WhatIsGround;
bool grounded = false;
float groundRadius = 0.2f;
bool doubleJump = false;
public float jumpForce = 70f;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
void FixedUpdate(){
grounded = Physics2D.OverlapCircle (GroundCheck.position, groundRadius, WhatIsGround);
anim.SetBool ("Ground", grounded);
if (grounded)
doubleJump = false;
if (!grounded)
return;
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs(move));
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Update()
{
if ((grounded || !doubleJump) && Input.GetKeyDown (KeyCode.UpArrow)) {
anim.SetBool("Ground",false);
rigidbody2D.AddForce(new Vector2(0,jumpForce));
if(!doubleJump && !grounded)
doubleJump = true;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.name == "GoldCoin")
Destroy (collision.gameObject);
}
void Flip(){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Thanks!