Question by
georgetas123 · Jun 09, 2019 at 10:12 PM ·
c#unity 5
Check if 2 objects from an array overlap and change their position on y if they do,Check if 2 objects in an array overlap and change their position if they do
using System.Linq; using UnityEngine; using Random = UnityEngine.Random;
public class SpikeControllerLeft : PersistentSingleton { [SerializeField] private GameObject[] spikesLeft;
private float yPosition;
private Vector3 desiredPosition;
private void Start()
{
foreach (var t in spikesLeft)
{
yPosition = Random.Range(-7, 7);
var position = t.transform.position;
desiredPosition = new Vector3(position.x, yPosition, position.z);
}
}
private bool CheckOverlap(GameObject o1, GameObject o2)
{
return spikesLeft.Select(t => t.GetComponent<Collider>().bounds.Intersects(t.GetComponent<Collider>().bounds)).FirstOrDefault();
}
public void StartRandomizingLeftSpikesPosition()
{
foreach (var t in spikesLeft)
{
foreach (var t1 in spikesLeft)
{
if (!CheckOverlap(t, t1)) continue;
yPosition = Random.Range(-7, 7);
var position = t1.transform.position;
desiredPosition = new Vector3(position.x, yPosition, position.z);
t1.transform.position = desiredPosition;
}
}
}
public void MoveLeftsSpikesOut()
{
foreach (var t in spikesLeft)
{
var position = t.transform.position;
t.transform.position = new Vector3(position.x + 2, position.y, position.z);
}
}
public void MoveLeftSpikesIn()
{
foreach (var t in spikesLeft)
{
var position = t.transform.position;
t.transform.position = new Vector3(position.x - 2, position.y, position.z);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to use the results of a dice roll? 2 Answers
OnGUI will not show up? 1 Answer
Why isn't my object lerping ? C# 2 Answers
My audio doesn't play, I'm through my options... 1 Answer
Need help with c# 1 Answer