Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- THIS IS MY PLAYER SCRIPT:
- using System.Collections;
- using UnityEngine;
- public delegate void DeadEventHandler();
- public class Player : CharacterScript
- {
- private static Player instance;
- public event DeadEventHandler Dead;
- public static Player Instance
- {
- get
- {
- if (instance == null)
- {
- instance = GameObject.FindObjectOfType<Player>();
- }
- return instance;
- }
- }
- private bool immortal = false;
- [SerializeField]
- private float immortalTime;
- private float direction;
- private bool move;
- private float btnHorizontal;
- private SpriteRenderer spriteRenderer;
- [SerializeField]
- private Transform[] groundPoints;
- [SerializeField]
- private float groundRadius;
- [SerializeField]
- private LayerMask whatIsGround;
- [SerializeField]
- private bool airControl;
- [SerializeField]
- private float jumpForce;
- public Rigidbody2D MyRigidBody { get; set; }
- public bool Jump { get; set; }
- public bool OnGround { get; set; }
- public override bool IsDead
- {
- get
- {
- if (health <= 0)
- {
- OnDead();
- }
- return health <= 0;
- }
- }
- private Vector2 startPos;
- // Use this for initialization
- public override void Start()
- {
- Debug.Log("PlayerStart");
- base.Start();
- startPos = transform.position;
- spriteRenderer = GetComponent<SpriteRenderer>();
- MyRigidBody = GetComponent<Rigidbody2D>();
- }
- void Update()
- {
- if(!TakingDamage && !IsDead)
- {
- if (transform.position.y <= -14f)
- {
- GameControlScript.health -= 1;
- Death();
- }
- }
- PlayerRaycast();
- HandleInput();
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- if (!TakingDamage && !IsDead)
- {
- float horizontal = Input.GetAxis("Horizontal");
- OnGround = IsGrounded();
- if(move)
- {
- this.btnHorizontal = Mathf.Lerp(btnHorizontal, direction, Time.deltaTime * 2);
- HandleMovement(btnHorizontal);
- Flip(direction);
- }
- else
- {
- HandleMovement(horizontal);
- Flip(horizontal);
- }
- HandleLayers();
- }
- }
- public void OnDead()
- {
- if (Dead != null)
- {
- Dead();
- }
- }
- private void HandleMovement(float horizontal)
- {
- if (MyRigidBody.velocity.y < 0)
- {
- MyAnimator.SetBool("land", true);
- }
- if (Jump && MyRigidBody.velocity.y == 0)
- {
- MyRigidBody.AddForce(new Vector2(0, jumpForce));
- }
- MyRigidBody.velocity = new Vector2(horizontal * movementSpeed, MyRigidBody.velocity.y);
- MyAnimator.SetFloat("speed", Mathf.Abs(horizontal));
- }
- private void HandleInput()
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- MyAnimator.SetTrigger("jump");
- }
- }
- private void Flip(float horizontal)
- {
- if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
- {
- ChangeDirection();
- }
- }
- private bool IsGrounded()
- {
- if (MyRigidBody.velocity.y <= 0)
- {
- foreach (Transform point in groundPoints)
- {
- Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
- for (int i = 0; i < colliders.Length; i++)
- {
- if (colliders[i].gameObject != gameObject)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- private void HandleLayers()
- {
- if (!OnGround)
- {
- MyAnimator.SetLayerWeight(1, 1);
- }
- else
- {
- MyAnimator.SetLayerWeight(1, 0);
- }
- }
- void PlayerRaycast()
- {
- RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down);
- if (hit != null && hit.collider != null && hit.distance < 2.5f && hit.collider.tag == "enemy")
- {
- GetComponent<Rigidbody2D>().AddForce(Vector2.up * 30);
- hit.collider.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 200);
- hit.collider.gameObject.GetComponent<Rigidbody2D>().gravityScale = 10;
- hit.collider.gameObject.GetComponent<Rigidbody2D>().freezeRotation = false;
- hit.collider.gameObject.GetComponent<CapsuleCollider2D>().enabled = false;
- hit.collider.gameObject.GetComponent<EnemyMove>().enabled = false;
- }
- if (hit != null && hit.collider != null && hit.distance < 2.5f && hit.collider.tag != "enemy")
- {
- OnGround = true;
- }
- }
- public IEnumerator IndicateImmortal()
- {
- while (immortal)
- {
- spriteRenderer.enabled = false;
- yield return new WaitForSeconds(.1f);
- spriteRenderer.enabled = true;
- yield return new WaitForSeconds(.1f);
- }
- }
- public override IEnumerator TakeDamage()
- {
- if (!immortal)
- {
- health -= 1;
- if (!IsDead)
- {
- MyAnimator.SetTrigger("damage");
- immortal = true;
- StartCoroutine(IndicateImmortal());
- yield return new WaitForSeconds(immortalTime);
- immortal = false;
- }
- else
- {
- MyAnimator.SetLayerWeight(1, 0);
- MyAnimator.SetTrigger("die");
- }
- }
- }
- public override void Death()
- {
- MyRigidBody.velocity = Vector2.zero;
- MyAnimator.SetTrigger("idle");
- health -= 1;
- transform.position = startPos;
- }
- public void BtnJump()
- {
- MyAnimator.SetTrigger("jump");
- }
- public void BtnMove(float direction)
- {
- this.direction = direction;
- this.move = true;
- }
- public void BtnStopMove()
- {
- this.direction = 0;
- this.btnHorizontal = 0;
- this.move = false;
- }
- }
- THIS IS MY CHARACTER SCRIPT:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public abstract class CharacterScript : MonoBehaviour {
- [SerializeField]
- protected float movementSpeed;
- [SerializeField]
- protected int health;
- public abstract bool IsDead { get; }
- public bool TakingDamage { get; set; }
- public Animator MyAnimator { get; private set; }
- protected bool facingRight;
- // Use this for initialization
- public virtual void Start()
- {
- Debug.Log("CharStart");
- facingRight = true;
- MyAnimator = GetComponent<Animator>();
- }
- // Update is called once per frame
- void Update () {
- }
- public abstract IEnumerator TakeDamage();
- public abstract void Death();
- public void ChangeDirection()
- {
- facingRight = !facingRight;
- transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
- }
- public virtual void OnTriggerEnter2D(Collider2D other)
- {
- if (other.tag == "enemy")
- {
- StartCoroutine(TakeDamage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement