Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerControllerAlva : MonoBehaviour {
- public float speed = 10, jumpVelocity = 10;
- Transform myTrans, tagGround;
- Rigidbody2D myBody;
- public bool canMoveInAir = true;
- public LayerMask playerMask;
- private float hInput = 0;
- public bool facingRight;
- public float horizontal;
- AnimationPlayerController myAnim;
- public bool isGrounded = false;
- private bool defenseMode = false;
- void Start()
- {
- facingRight = true;
- myBody = this.GetComponent<Rigidbody2D>();
- myTrans = this.transform;
- tagGround = GameObject.Find(this.name + "/tag_ground").transform;
- //myAnim = AnimationPlayerController.instance;
- }
- void FixedUpdate()
- {
- isGrounded = Physics2D.Linecast(myTrans.position, tagGround.position, playerMask);
- //myAnim.UpdateIsGrounded(isGrounded);
- DefenseMode(defenseMode);
- Flip(horizontal);
- horizontal = Input.GetAxis("Horizontal");
- #if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
- hInput = Input.GetAxisRaw("Horizontal");
- //myAnim.UpdateSpeed(hInput);
- if (Input.GetButtonDown("Jump"))
- {
- Jump();
- }
- #endif
- Move(hInput);
- }
- public void DefenseMode(bool mode)
- {
- if (Input.GetKeyDown(KeyCode.LeftControl))
- {
- this.gameObject.GetComponent<Transform>().localScale = new Vector3(2, 1, 1);
- gameObject.GetComponent<PlayController>().speed = 3;
- gameObject.GetComponent<PlayController>().jumpVelocity = 3;
- gameObject.GetComponent<Shooting>().enabled = false;
- defenseMode = true;
- this.tag = "PlayerDefense";
- }
- if (Input.GetKeyUp(KeyCode.LeftControl))
- {
- this.gameObject.GetComponent<Transform>().localScale = new Vector3(1, 1, 1);
- gameObject.GetComponent<PlayController>().speed = 5;
- gameObject.GetComponent<PlayController>().jumpVelocity = 5;
- gameObject.GetComponent<Shooting>().enabled = true;
- defenseMode = false;
- this.tag = "PlayerAttack";
- }
- }
- void Move(float horizontalInput)
- {
- if (!canMoveInAir && !isGrounded)
- return;
- Vector2 moveVel = myBody.velocity;
- moveVel.x = horizontalInput * speed;
- myBody.velocity = moveVel;
- }
- public void Jump()
- {
- if (isGrounded)
- {
- myBody.velocity += jumpVelocity * Vector2.up;
- }
- }
- public void StartMoving(float horizontalInput)
- {
- hInput = horizontalInput;
- horizontal = horizontalInput;
- //myAnim.UpdateSpeed(horizontalInput);
- }
- private void Flip(float horizontal)
- {
- if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
- {
- facingRight = !facingRight;
- Vector3 theScale = transform.localScale;
- theScale.x *= -1;
- transform.localScale = theScale;
- }
- }
- //private void OnCollisionEnter2D(Collision2D collision)
- //{
- // if(collision.gameObject.tag == "Item")
- // {
- // GameManager.Instance.CollectedItems--;
- // GameManager.Instance.amountOfItemsNeeded--;
- // Destroy(collision.gameObject);
- // }
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement