Advertisement
Guest User

I don't understand

a guest
Jul 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. class CentipedeScript : MonoBehaviour
  7. {
  8.     public Sprite headSprite;
  9.     public Sprite bodySprite;
  10.     public GameObject mushroom;
  11.  
  12.     private float height;
  13.     private float width;
  14.     private float headCentipedeRadius = 0.5f;
  15.     private bool leftDirection = false;
  16.     private bool downDirection;
  17.     private bool isHead = false;
  18.     private float speed;
  19.     private GameObject target;
  20.     private bool isDead = false;
  21.     private Vector2 previousPosition;
  22.     private Vector2 moveDownToThisPoint;
  23.     private Rigidbody2D rigidbody;
  24.  
  25.     void Start()
  26.     {
  27.         rigidbody = GetComponent<Rigidbody2D>();
  28.         if (isHead) GetComponent<SpriteRenderer>().sprite = headSprite;
  29.         else GetComponent<SpriteRenderer>().sprite = bodySprite;
  30.  
  31.         height = 2f * Camera.main.orthographicSize;
  32.         width = height * Camera.main.aspect;
  33.     }
  34.  
  35.     void Update()
  36.     {
  37.         if (isHead && (transform.position.x - headCentipedeRadius <= -width / 2 || transform.position.x + headCentipedeRadius >= width / 2) && !downDirection)
  38.         {
  39.             ChangeDirection();
  40.         }      
  41.         if (isHead) MoveHead();
  42.         else MoveBody();
  43.     }
  44.  
  45.     void MoveHead()
  46.     {
  47.         if (downDirection)
  48.         {
  49.             if ((Vector2)transform.position == previousPosition)
  50.                 CentipedeStuck();
  51.             previousPosition = new Vector2(transform.position.x, transform.position.y);
  52.             MoveDown();
  53.         }
  54.         else
  55.         {
  56.             moveDownToThisPoint = new Vector2(transform.position.x, transform.position.y - 1);
  57.             MoveLeftOrRight();
  58.         }
  59.     }
  60.  
  61.     void CentipedeStuck()
  62.     {
  63.         downDirection = false;
  64.         MoveLeftOrRight();
  65.     }
  66.  
  67.     void MoveDown()
  68.     {
  69.         if(transform.position.y > moveDownToThisPoint.y)
  70.         {
  71.             rigidbody.velocity = Vector2.down * speed;
  72.             transform.rotation = Quaternion.AngleAxis(GetAngleRotation(rigidbody.velocity.normalized), Vector3.forward);
  73.         }
  74.         else if (transform.position.y <= moveDownToThisPoint.y)
  75.         {
  76.             leftDirection = !leftDirection;
  77.             downDirection = false;
  78.             MoveLeftOrRight();        
  79.         }
  80.     }
  81.  
  82.     void MoveLeftOrRight()
  83.     {
  84.         if (leftDirection)
  85.             rigidbody.velocity = Vector2.left * speed;
  86.         else
  87.             rigidbody.velocity = Vector2.right * speed;
  88.         transform.rotation = Quaternion.AngleAxis(GetAngleRotation(rigidbody.velocity.normalized), Vector3.forward);
  89.     }
  90.  
  91.     void ChangeDirection()
  92.     {
  93.         downDirection = true;
  94.     }
  95.    
  96.     //Part for Body Moving
  97.     void MoveBody()
  98.     {
  99.  
  100.     }
  101.  
  102.     private void OnTriggerEnter2D(Collider2D other)
  103.     {
  104.         if (other.tag == "Mushroom" && isHead && !downDirection)
  105.         {
  106.             Debug.Log("MushroomCollision");
  107.             ChangeDirection();
  108.         }
  109.         if (other.tag == "Shot")
  110.         {
  111.             Destroy(other.gameObject);
  112.             Destroy(gameObject);
  113.             Instantiate(mushroom, transform.position, Quaternion.identity);
  114.         }
  115.     }
  116.  
  117.     float GetAngleRotation(Vector2 direction)
  118.     {
  119.         return Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90;
  120.     }
  121.  
  122.     public void SetHead()
  123.     {
  124.         isHead = true;
  125.     }
  126.  
  127.     public void SetCentipedeSpeed(float speed)
  128.     {
  129.         this.speed = speed;
  130.     }
  131.  
  132.     public void SetTarget(GameObject target)
  133.     {
  134.         this.target = target;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement