Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- class CentipedeScript : MonoBehaviour
- {
- public Sprite headSprite;
- public Sprite bodySprite;
- public GameObject mushroom;
- private float height;
- private float width;
- private float headCentipedeRadius = 0.5f;
- private bool leftDirection = false;
- private bool downDirection;
- private bool isHead = false;
- private float speed;
- private GameObject target;
- private bool isDead = false;
- private Vector2 previousPosition;
- private Vector2 moveDownToThisPoint;
- private Rigidbody2D rigidbody;
- void Start()
- {
- rigidbody = GetComponent<Rigidbody2D>();
- if (isHead) GetComponent<SpriteRenderer>().sprite = headSprite;
- else GetComponent<SpriteRenderer>().sprite = bodySprite;
- height = 2f * Camera.main.orthographicSize;
- width = height * Camera.main.aspect;
- }
- void Update()
- {
- if (isHead && (transform.position.x - headCentipedeRadius <= -width / 2 || transform.position.x + headCentipedeRadius >= width / 2) && !downDirection)
- {
- ChangeDirection();
- }
- if (isHead) MoveHead();
- else MoveBody();
- }
- void MoveHead()
- {
- if (downDirection)
- {
- if ((Vector2)transform.position == previousPosition)
- CentipedeStuck();
- previousPosition = new Vector2(transform.position.x, transform.position.y);
- MoveDown();
- }
- else
- {
- moveDownToThisPoint = new Vector2(transform.position.x, transform.position.y - 1);
- MoveLeftOrRight();
- }
- }
- void CentipedeStuck()
- {
- downDirection = false;
- MoveLeftOrRight();
- }
- void MoveDown()
- {
- if(transform.position.y > moveDownToThisPoint.y)
- {
- rigidbody.velocity = Vector2.down * speed;
- transform.rotation = Quaternion.AngleAxis(GetAngleRotation(rigidbody.velocity.normalized), Vector3.forward);
- }
- else if (transform.position.y <= moveDownToThisPoint.y)
- {
- leftDirection = !leftDirection;
- downDirection = false;
- MoveLeftOrRight();
- }
- }
- void MoveLeftOrRight()
- {
- if (leftDirection)
- rigidbody.velocity = Vector2.left * speed;
- else
- rigidbody.velocity = Vector2.right * speed;
- transform.rotation = Quaternion.AngleAxis(GetAngleRotation(rigidbody.velocity.normalized), Vector3.forward);
- }
- void ChangeDirection()
- {
- downDirection = true;
- }
- //Part for Body Moving
- void MoveBody()
- {
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.tag == "Mushroom" && isHead && !downDirection)
- {
- Debug.Log("MushroomCollision");
- ChangeDirection();
- }
- if (other.tag == "Shot")
- {
- Destroy(other.gameObject);
- Destroy(gameObject);
- Instantiate(mushroom, transform.position, Quaternion.identity);
- }
- }
- float GetAngleRotation(Vector2 direction)
- {
- return Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90;
- }
- public void SetHead()
- {
- isHead = true;
- }
- public void SetCentipedeSpeed(float speed)
- {
- this.speed = speed;
- }
- public void SetTarget(GameObject target)
- {
- this.target = target;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement