GoodNoodle

ShipMove

Apr 5th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ShipMove : MonoBehaviour
  6. {
  7.  
  8.     public Transform[] points;
  9.  
  10.     private int TarPoint;
  11.  
  12.     public float speed;
  13.     public GameObject objToSpawn;
  14.     PlayerController player;
  15.  
  16.     // Start is called before the first frame update
  17.     void Start()
  18.     {
  19.         player = FindObjectOfType<PlayerController>();
  20.     }
  21.  
  22.     // Update is called once per frame
  23.     void Update()
  24.     {
  25.         player.transform.position = transform.position;
  26.         player.canMove = false;
  27.         StartCoroutine(MoveShip());
  28.     }
  29.  
  30.     public IEnumerator MoveShip()
  31.     {
  32.        
  33.         yield return new WaitForSeconds(2f);
  34.  
  35.         if (transform.position == points[TarPoint].position)
  36.         {
  37.             IncreaseInt();
  38.         }
  39.         transform.position = Vector3.MoveTowards(transform.position, points[TarPoint].position, speed * Time.deltaTime);
  40.     }
  41.  
  42.     private void IncreaseInt()
  43.     {
  44.         TarPoint++;
  45.  
  46.         if (TarPoint >= points.Length)
  47.         {
  48.             TarPoint = 0;
  49.            
  50.         }
  51.     }
  52.  
  53.     private void OnCollisionStay2D(Collision2D other)
  54.     {
  55.      
  56.  
  57.     }
  58.  
  59.     private void OnCollisionEnter2D(Collision2D collision)
  60.     {
  61.         if (collision.gameObject.tag == "Player")
  62.         {
  63.             player.transform.position = transform.position;
  64.             StartCoroutine(MoveShip());
  65.         }
  66.     }
  67.  
  68.     private void OnCollisionExit2D(Collision2D collision)
  69.     {
  70.         collision.transform.SetParent(null);
  71.     }
  72. }
  73.  
Add Comment
Please, Sign In to add comment