Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public enum State
- {
- Normal,
- Damaged,
- Atack,
- Jump,
- speaking
- }
- public class controller2D : MonoBehaviour {
- public float speed=1f;
- public float jumpPower=5f;
- Rigidbody2D m_rigidbody2D;
- public LayerMask groundLayer;
- public float up=1f;
- public float down=0.1f;
- public Vector2 backwardForce = new Vector2(-4.5f, 5.4f);
- public GameObject weapon;
- public bool canMove=false;
- Animator objanimator;
- public State playerstate = State.Normal;
- bool grounded;
- void Start () {
- m_rigidbody2D = GetComponent<Rigidbody2D> ();
- objanimator=GetComponent<Animator>();
- }
- void FixedUpdate()
- {
- grounded = Physics2D.Linecast(
- transform.position + transform.up * up,
- transform.position - transform.up * down,
- groundLayer
- );
- }
- void Update () {
- if(canMove){
- if (playerstate != State.Damaged)
- {
- float x = Input.GetAxis("Horizontal");
- bool jump = Input.GetKeyDown(KeyCode.Space);
- Move(x, jump);
- }
- }
- }
- void Move(float move, bool jump)
- {
- if (Mathf.Abs(move) > 0)
- {
- Quaternion rot = transform.rotation;
- transform.rotation = Quaternion.Euler(rot.x, Mathf.Sign(move) == 1 ? 90 : 270, rot.z);
- }else{
- if(Input.GetKeyDown(KeyCode.E)){
- objanimator.Play("Atack");
- }
- }
- m_rigidbody2D.velocity = new Vector2(move * speed, m_rigidbody2D.velocity.y);
- objanimator.SetFloat("run", Mathf.Abs(move));
- if (jump&&grounded)
- {
- m_rigidbody2D.AddForce(Vector2.up * jumpPower);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement