using UnityEngine;
public class Example : MonoBehaviour {
public static Example instance = null;
private void Awake() {
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
// Ваш код
}
Debug.Log(hit.transform.gameObject.name);
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using UnityEngine;
using UnityEngine.Events;
public class TimedTriggers : MonoBehaviour {
[Serializable]
public class TimedEvent {
public float delay;
public bool looping;
public UnityEvent onEvent;
}
public class EventData {
public TimedEvent timedEvent;
public float timer;
public bool readyToPlay;
public EventData(TimedEvent t) {
timedEvent = t;
}
public bool Tick(float deltaTime) {
if (timer < timedEvent.delay) {
timer += deltaTime;
return false;
}
if (timedEvent.looping && !readyToPlay)
readyToPlay = true;
if (readyToPlay) {
readyToPlay = false;
timer = 0f;
return true;
}
return false;
}
}
[Min(0.01f)][SerializeField] private float m_timeStep = 0.1f;
[SerializeField] private List<TimedEvent> m_events = new List<TimedEvent>();
private List<EventData> m_eventData = new List<EventData>();
private void Awake() {
foreach (TimedEvent e in m_events)
m_eventData.Add(new EventData(e));
if (m_eventData.Count > 0)
StartCoroutine(TimeRoutine());
}
private IEnumerator TimeRoutine() {
while (true) {
foreach (EventData e in m_eventData)
if (e.Tick(m_timeStep))
e.timedEvent.onEvent.Invoke();
yield return new WaitForSeconds(m_timeStep);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeMovement : MonoBehaviour {
[Min(0.1f)][SerializeField] private float moveSpeed = 15f;
private bool isMoving;
private void Update() {
if (isMoving)
return;
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
Move(Vector3.forward);
else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
Move(Vector3.left);
else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
Move(Vector3.back);
else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
Move(Vector3.right);
}
private void Move(Vector3 direction) {
if (!CheckPositionIsInObstacle(transform.position + direction))
StartCoroutine(MoveRoutine(direction));
}
private IEnumerator MoveRoutine(Vector3 direction) {
Vector3 anchor = transform.position + (Vector3.down + direction) * (transform.localScale.x / 2f);
Vector3 axis = Vector3.Cross(Vector3.up, direction);
isMoving = true;
for (int i = 0; i < (90f / moveSpeed); i++) {
transform.RotateAround(anchor, axis, moveSpeed);
yield return new WaitForSeconds(0.01f);
}
isMoving = false;
}
private bool CheckPositionIsInObstacle(Vector3 position) {
Collider[] obstacles = FindObjectsOfType<Collider>();
if (obstacles.Length > 0)
foreach (Collider c in obstacles)
if (c != null && c.bounds.Contains(position))
return true;
return false;
}
}