Advertisement
eyeksel

Untitled

Feb 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.83 KB | None | 0 0
  1. THIS IS MY PLAYER SCRIPT:
  2.  
  3. using System.Collections;
  4. using UnityEngine;
  5.  
  6. public delegate void DeadEventHandler();
  7.  
  8. public class Player : CharacterScript
  9. {
  10.  
  11.  
  12.     private static Player instance;
  13.  
  14.     public event DeadEventHandler Dead;
  15.  
  16.  
  17.     public static Player Instance
  18.     {
  19.         get
  20.         {
  21.             if (instance == null)
  22.             {
  23.                 instance = GameObject.FindObjectOfType<Player>();
  24.             }
  25.             return instance;
  26.         }
  27.     }
  28.  
  29.  
  30.     private bool immortal = false;
  31.  
  32.     [SerializeField]
  33.     private float immortalTime;
  34.  
  35.     private float direction;
  36.  
  37.     private bool move;
  38.  
  39.     private float btnHorizontal;
  40.  
  41.     private SpriteRenderer spriteRenderer;
  42.  
  43.  
  44.     [SerializeField]
  45.     private Transform[] groundPoints;
  46.  
  47.     [SerializeField]
  48.     private float groundRadius;
  49.  
  50.     [SerializeField]
  51.     private LayerMask whatIsGround;
  52.  
  53.     [SerializeField]
  54.     private bool airControl;
  55.  
  56.     [SerializeField]
  57.     private float jumpForce;
  58.  
  59.    
  60.  
  61.     public Rigidbody2D MyRigidBody { get; set; }
  62.  
  63.     public bool Jump { get; set; }
  64.  
  65.     public bool OnGround { get; set; }
  66.  
  67.     public override bool IsDead
  68.     {
  69.         get
  70.         {
  71.             if (health <= 0)
  72.             {
  73.                 OnDead();
  74.             }
  75.            
  76.             return health <= 0;
  77.         }
  78.     }
  79.  
  80.     private Vector2 startPos;
  81.  
  82.     // Use this for initialization
  83.      public override void Start()
  84.     {
  85.  
  86.         Debug.Log("PlayerStart");
  87.  
  88.         base.Start();
  89.  
  90.         startPos = transform.position;
  91.  
  92.         spriteRenderer = GetComponent<SpriteRenderer>();
  93.  
  94.         MyRigidBody = GetComponent<Rigidbody2D>();
  95.  
  96.        
  97.  
  98.     }
  99.     void Update()
  100.     {  
  101.         if(!TakingDamage && !IsDead)
  102.         {
  103.             if (transform.position.y <= -14f)
  104.             {
  105.                 GameControlScript.health -= 1;
  106.                 Death();
  107.             }
  108.         }
  109.  
  110.         PlayerRaycast();
  111.         HandleInput();
  112.     }
  113.     // Update is called once per frame
  114.     void FixedUpdate()
  115.     {
  116.         if (!TakingDamage && !IsDead)
  117.         {
  118.             float horizontal = Input.GetAxis("Horizontal");
  119.  
  120.             OnGround = IsGrounded();
  121.  
  122.             if(move)
  123.             {
  124.                 this.btnHorizontal = Mathf.Lerp(btnHorizontal, direction, Time.deltaTime * 2);
  125.                 HandleMovement(btnHorizontal);
  126.                 Flip(direction);
  127.             }
  128.             else
  129.             {
  130.                 HandleMovement(horizontal);
  131.  
  132.                 Flip(horizontal);
  133.             }
  134.            
  135.             HandleLayers();
  136.         }
  137.  
  138.     }
  139.  
  140.     public void OnDead()
  141.     {
  142.         if (Dead != null)
  143.         {
  144.             Dead();
  145.         }
  146.     }
  147.     private void HandleMovement(float horizontal)
  148.     {
  149.  
  150.  
  151.         if (MyRigidBody.velocity.y < 0)
  152.         {
  153.             MyAnimator.SetBool("land", true);
  154.         }
  155.         if (Jump && MyRigidBody.velocity.y == 0)
  156.         {
  157.             MyRigidBody.AddForce(new Vector2(0, jumpForce));
  158.         }
  159.  
  160.         MyRigidBody.velocity = new Vector2(horizontal * movementSpeed, MyRigidBody.velocity.y);
  161.  
  162.         MyAnimator.SetFloat("speed", Mathf.Abs(horizontal));
  163.  
  164.     }
  165.  
  166.     private void HandleInput()
  167.     {
  168.         if (Input.GetKeyDown(KeyCode.Space))
  169.         {
  170.             MyAnimator.SetTrigger("jump");
  171.         }
  172.     }
  173.     private void Flip(float horizontal)
  174.     {
  175.         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
  176.         {
  177.             ChangeDirection();
  178.         }
  179.     }
  180.  
  181.  
  182.     private bool IsGrounded()
  183.     {
  184.         if (MyRigidBody.velocity.y <= 0)
  185.         {
  186.             foreach (Transform point in groundPoints)
  187.             {
  188.                 Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
  189.  
  190.                 for (int i = 0; i < colliders.Length; i++)
  191.                 {
  192.                     if (colliders[i].gameObject != gameObject)
  193.                     {
  194.  
  195.                         return true;
  196.                     }
  197.                 }
  198.             }
  199.         }
  200.         return false;
  201.     }
  202.  
  203.     private void HandleLayers()
  204.     {
  205.         if (!OnGround)
  206.         {
  207.             MyAnimator.SetLayerWeight(1, 1);
  208.         }
  209.         else
  210.         {
  211.             MyAnimator.SetLayerWeight(1, 0);
  212.         }
  213.  
  214.     }
  215.     void PlayerRaycast()
  216.     {
  217.         RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down);
  218.         if (hit != null && hit.collider != null && hit.distance < 2.5f && hit.collider.tag == "enemy")
  219.         {
  220.             GetComponent<Rigidbody2D>().AddForce(Vector2.up * 30);
  221.             hit.collider.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 200);
  222.             hit.collider.gameObject.GetComponent<Rigidbody2D>().gravityScale = 10;
  223.             hit.collider.gameObject.GetComponent<Rigidbody2D>().freezeRotation = false;
  224.             hit.collider.gameObject.GetComponent<CapsuleCollider2D>().enabled = false;
  225.             hit.collider.gameObject.GetComponent<EnemyMove>().enabled = false;
  226.         }
  227.         if (hit != null && hit.collider != null && hit.distance < 2.5f && hit.collider.tag != "enemy")
  228.         {
  229.             OnGround = true;
  230.         }
  231.    
  232.     }
  233.  
  234.  
  235.     public IEnumerator IndicateImmortal()
  236.     {
  237.         while (immortal)
  238.         {
  239.             spriteRenderer.enabled = false;
  240.  
  241.             yield return new WaitForSeconds(.1f);
  242.  
  243.             spriteRenderer.enabled = true;
  244.  
  245.             yield return new WaitForSeconds(.1f);
  246.         }
  247.     }
  248.  
  249.     public override IEnumerator TakeDamage()
  250.     {
  251.  
  252.         if (!immortal)
  253.         {
  254.             health -= 1;
  255.  
  256.             if (!IsDead)
  257.             {
  258.                 MyAnimator.SetTrigger("damage");
  259.                 immortal = true;
  260.  
  261.                 StartCoroutine(IndicateImmortal());
  262.  
  263.                 yield return new WaitForSeconds(immortalTime);
  264.  
  265.                 immortal = false;
  266.             }
  267.             else
  268.             {
  269.                 MyAnimator.SetLayerWeight(1, 0);
  270.                 MyAnimator.SetTrigger("die");
  271.             }
  272.         }
  273.     }
  274.  
  275.     public override void Death()
  276.     {
  277.         MyRigidBody.velocity = Vector2.zero;
  278.         MyAnimator.SetTrigger("idle");
  279.         health -= 1;
  280.         transform.position = startPos;
  281.     }
  282.  
  283.     public void BtnJump()
  284.     {
  285.         MyAnimator.SetTrigger("jump");
  286.        
  287.  
  288.     }
  289.     public void BtnMove(float direction)
  290.     {
  291.         this.direction = direction;
  292.         this.move = true;
  293.     }
  294.     public void BtnStopMove()
  295.     {
  296.         this.direction = 0;
  297.         this.btnHorizontal = 0;
  298.         this.move = false;
  299.     }
  300.  
  301. }
  302.  
  303.  
  304. THIS IS MY CHARACTER SCRIPT:
  305.  
  306.  
  307. using System.Collections;
  308. using System.Collections.Generic;
  309. using UnityEngine;
  310.  
  311. public abstract class CharacterScript : MonoBehaviour {
  312.  
  313.  
  314.     [SerializeField]
  315.     protected float movementSpeed;
  316.  
  317.     [SerializeField]
  318.     protected int health;
  319.  
  320.     public abstract bool IsDead { get; }
  321.  
  322.     public bool TakingDamage { get; set; }
  323.  
  324.     public Animator MyAnimator { get; private set; }
  325.  
  326.     protected bool facingRight;
  327.  
  328.     // Use this for initialization
  329.    public virtual void Start()
  330.     {
  331.         Debug.Log("CharStart");
  332.  
  333.         facingRight = true;
  334.  
  335.         MyAnimator = GetComponent<Animator>();
  336.     }
  337.    
  338.     // Update is called once per frame
  339.     void Update () {
  340.        
  341.  
  342.     }
  343.     public abstract IEnumerator TakeDamage();
  344.  
  345.     public abstract void Death();
  346.    
  347.  
  348.     public void ChangeDirection()
  349.     {
  350.         facingRight = !facingRight;
  351.  
  352.         transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
  353.     }
  354.  
  355.     public virtual void OnTriggerEnter2D(Collider2D other)
  356.     {
  357.         if (other.tag == "enemy")
  358.         {
  359.             StartCoroutine(TakeDamage());
  360.         }
  361.     }
  362.  
  363.    
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement