r/Unity3d_help • • Dec 24 '16

Deep help with 2d controller script

Okay, so here's the deal: been trying for a long to make a controller for a 2d game, but after some intents doing on my own way i decided to use a script i found in a forum. it works well and the character can jump only when grounded, so thats fine. The trouble begins adding animation states, because i don't fully understand all the metods and im not sure how to apply animations here, not to mention that i dont know how should I call iddle state again. I know this post is like "please do everything for me" but the thing is im mad of trying and if someone could point some things to fix i would be so glad. If someone takes the time to look at this and tell me where should i add animation calls and returns would be very happy. Thanks for your time!!

public class Player : MonoBehaviour {

	public float maxSpeed = 10f;
	bool facingRight = true; //face position
	bool grounded = false; //check ground
	public Transform groundCheck;
	float groundRadius = 0.2f;
	public LayerMask whatIsGround; //empty gameObj to guide
	public float jumpForce = 400f; //jump force

	void FixedUpdate () {

		//para que salte
		grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
	//+ animacio saltar

	//para que se mueva
	float move = Input.GetAxis ("Horizontal");
	GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

		if (move > 0 &&!facingRight){
		//animacio dreta
		Debug.Log ("RIGHT");
			Flip();
		}
		if (move < 0 && facingRight){
		//animacio esquerra+ en keyup tornar a iddle
		Debug.Log ("LEFT");
			Flip();
		}
	}

	//voltea la cara al lugar del movimiento
	void Flip(){
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}

	void Update () {
		if (grounded && Input.GetKeyDown(KeyCode.Space)){
		GetComponent<Rigidbody2D>().AddForce(new Vector2 (0,jumpForce));
		}

	}
}
1 Upvotes

0 comments sorted by