Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Player : MonoBehaviour
- {
- private Rigidbody2D _myRigidbody;
- public float speed;
- private Animator _anim;
- private bool _facingRight = true;
- private bool _canJump = true;
- public Transform startPos;
- public Transform endPos;
- public LayerMask groundLayer;
- // Use this for initialization
- void Start ()
- {
- _myRigidbody = this.rigidbody2D;
- _anim = GetComponent<Animator> ();
- }
- // Update is called once per frame
- void Update ()
- {
- float move = Input.GetAxisRaw("Horizontal");
- _anim.SetFloat ("speed", Mathf.Abs(move));
- _myRigidbody.velocity = new Vector2(move * speed, _myRigidbody.velocity.y);
- if (_facingRight == true && move < 0)
- {
- _facingRight = false;
- transform.rotation = Quaternion.Euler(transform.rotation.x, 180, transform.rotation.z);
- }
- else if (_facingRight == false && move > 0)
- {
- _facingRight = true;
- transform.rotation = Quaternion.Euler(transform.rotation.x, 0, transform.rotation.z);
- }
- Debug.DrawLine (startPos.position, endPos.position);
- RaycastHit2D hitInfo = Physics2D.Linecast(startPos.position, endPos.position,groundLayer.value);
- if (hitInfo.collider != null)
- {
- _anim.SetBool ("Jump", false);
- _canJump = true;
- }
- if (Input.GetKeyDown (KeyCode.Space) && _canJump == true)
- {
- _anim.SetBool ("Jump", true);
- _canJump = false;
- _myRigidbody.velocity = new Vector2 (_myRigidbody.velocity.x, 5);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement