Guest User

InfiniteChunkBackground

a guest
Sep 6th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DotProduct : MonoBehaviour
  6. {  
  7.     [SerializeField] private protected Player player;
  8.  
  9.     [SerializeField] private Chunk chunkA;
  10.     [SerializeField] private Chunk chunkB;
  11.     [SerializeField] private Chunk chunkC;
  12.  
  13.     public List<Chunk> chunks = new List<Chunk>();
  14.  
  15.     Chunk currentChunk;
  16.     Chunk previousChunk;
  17.     Chunk nextChunk;
  18.  
  19.     private float dotProductThreshold = 0f;
  20.     public bool goingUp;
  21.  
  22.     [SerializeField] private float chunkSize = 20f;
  23.  
  24.     void Start()
  25.     {
  26.         chunks.Add(chunkA);
  27.         chunks.Add(chunkB);
  28.         chunks.Add(chunkC);
  29.     }
  30.  
  31.     void OnEnable()
  32.     {
  33.         GameEvents.OnChunkEntered += OnChunkEntered;
  34.         GameEvents.OnChunkExited += OnChunkExited;
  35.     }
  36.  
  37.     void OnDisable()
  38.     {
  39.         GameEvents.OnChunkEntered -= OnChunkEntered;
  40.         GameEvents.OnChunkExited -= OnChunkExited;
  41.     }
  42.  
  43.     void OnChunkEntered(Chunk chunk)
  44.     {  
  45.         currentChunk = chunk;
  46.         FindNextChunk(currentChunk);
  47.         MoveNextChunk(nextChunk);
  48.     }
  49.  
  50.     void OnChunkExited(Chunk chunk)
  51.     {
  52.        
  53.     }
  54.  
  55.  
  56.     void Update()
  57.     {
  58.         goingUp = IsFacingUpwards();
  59.     }
  60.  
  61.     Chunk FindNextChunk(Chunk currentChunk)
  62.     {
  63.         int currentChunkIndex = chunks.IndexOf(currentChunk);
  64.         if (goingUp)
  65.         {
  66.             nextChunk = chunks[(currentChunkIndex + 1) % chunks.Count];
  67.         }
  68.         else
  69.         {
  70.             nextChunk = chunks[(currentChunkIndex - 1 + chunks.Count) % chunks.Count];
  71.         }
  72.  
  73.         return nextChunk;
  74.     }
  75.  
  76.  
  77.  
  78.     public bool IsFacingUpwards()
  79.     {
  80.         Vector2 forward = player.transform.up; // Get the player's up direction
  81.         Vector2 worldUp = Vector2.up; // Get the world's up direction
  82.  
  83.         float dotProduct = Vector2.Dot(forward, worldUp);
  84.  
  85.         return dotProduct > dotProductThreshold;
  86.     }
  87.  
  88.  
  89.     void MoveNextChunk(Chunk nextChunk)
  90.     {
  91.         if (goingUp)
  92.         {
  93.             nextChunk.transform.position = currentChunk.transform.position + new Vector3(0f, chunkSize, 0f);
  94.         }
  95.         else
  96.         {
  97.             nextChunk.transform.position = currentChunk.transform.position - new Vector3(0f, chunkSize, 0f);
  98.         }
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment