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 SlimeControll : MonoBehaviour
- {
- public float speed;
- public float distance;
- public int life = 3;
- private bool movingRight = true;
- public Transform wallDetectionRight;
- public Transform wallDetectionLeft;
- RaycastHit2D wallInfo;
- [SerializeField] private LayerMask layerMask;
- private float defaultSpeed;
- bool ragemode = false;
- SpriteRenderer sprite;
- Color defaultColor;
- // Start is called before the first frame update
- void Start()
- {
- defaultSpeed = speed;
- life = 3;
- sprite = GetComponent<SpriteRenderer>();
- defaultColor = sprite.color;
- }
- // Update is called once per frame
- void Update()
- {
- if (movingRight)
- {
- transform.Translate(Vector2.right * speed * Time.deltaTime);
- wallInfo = Physics2D.Raycast(wallDetectionRight.position, Vector2.right, distance, layerMask);
- }
- if (!movingRight)
- {
- transform.Translate(Vector2.left * speed * Time.deltaTime);
- wallInfo = Physics2D.Raycast(wallDetectionLeft.position, Vector2.left, distance, layerMask);
- }
- if (wallInfo.collider == true)
- {
- if (movingRight)
- {
- Flip();
- movingRight = false;
- }
- else
- {
- Flip();
- movingRight = true;
- }
- }
- }
- void OnTriggerEnter2D(Collider2D col)
- {
- if (col.CompareTag("bullet"))
- {
- life--;
- if (life <= 0)
- {
- Destroy(gameObject);
- } else if (ragemode == false)
- {
- ragemode = true;
- speed = defaultSpeed + 4;
- GameSystem.Instance.PlaySound("slimeAttack", GameSystem.Instance.enemySounds);
- Invoke("RevertSpeed", 3);
- if (col.gameObject.transform.position.x > transform.position.x)
- {
- if (!movingRight)
- {
- movingRight = true;
- Flip();
- }
- }
- else if (col.gameObject.transform.position.x < transform.position.x)
- {
- if (movingRight)
- {
- movingRight = false;
- Flip();
- }
- }
- }
- Blink();
- Destroy(col.gameObject);
- }
- }
- void RevertSpeed()
- {
- speed = defaultSpeed;
- ragemode = false;
- }
- void Flip()
- {
- Vector3 theScale = transform.localScale;
- theScale.x *= -1;
- transform.localScale = theScale;
- }
- void Blink()
- {
- sprite.material.color = Color.clear;
- Invoke("SetDefalutColor", 0.05f);
- }
- void SetDefalutColor()
- {
- sprite.material.color = defaultColor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement