Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DotProduct : MonoBehaviour
- {
- [SerializeField] private protected Player player;
- [SerializeField] private Chunk chunkA;
- [SerializeField] private Chunk chunkB;
- [SerializeField] private Chunk chunkC;
- public List<Chunk> chunks = new List<Chunk>();
- Chunk currentChunk;
- Chunk previousChunk;
- Chunk nextChunk;
- private float dotProductThreshold = 0f;
- public bool goingUp;
- [SerializeField] private float chunkSize = 20f;
- void Start()
- {
- chunks.Add(chunkA);
- chunks.Add(chunkB);
- chunks.Add(chunkC);
- }
- void OnEnable()
- {
- GameEvents.OnChunkEntered += OnChunkEntered;
- GameEvents.OnChunkExited += OnChunkExited;
- }
- void OnDisable()
- {
- GameEvents.OnChunkEntered -= OnChunkEntered;
- GameEvents.OnChunkExited -= OnChunkExited;
- }
- void OnChunkEntered(Chunk chunk)
- {
- currentChunk = chunk;
- FindNextChunk(currentChunk);
- MoveNextChunk(nextChunk);
- }
- void OnChunkExited(Chunk chunk)
- {
- }
- void Update()
- {
- goingUp = IsFacingUpwards();
- }
- Chunk FindNextChunk(Chunk currentChunk)
- {
- int currentChunkIndex = chunks.IndexOf(currentChunk);
- if (goingUp)
- {
- nextChunk = chunks[(currentChunkIndex + 1) % chunks.Count];
- }
- else
- {
- nextChunk = chunks[(currentChunkIndex - 1 + chunks.Count) % chunks.Count];
- }
- return nextChunk;
- }
- public bool IsFacingUpwards()
- {
- Vector2 forward = player.transform.up; // Get the player's up direction
- Vector2 worldUp = Vector2.up; // Get the world's up direction
- float dotProduct = Vector2.Dot(forward, worldUp);
- return dotProduct > dotProductThreshold;
- }
- void MoveNextChunk(Chunk nextChunk)
- {
- if (goingUp)
- {
- nextChunk.transform.position = currentChunk.transform.position + new Vector3(0f, chunkSize, 0f);
- }
- else
- {
- nextChunk.transform.position = currentChunk.transform.position - new Vector3(0f, chunkSize, 0f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment