hra vertical bird
This commit is contained in:
17
Assets/Scripts/VerticalBird/PohybPozadi.cs
Normal file
17
Assets/Scripts/VerticalBird/PohybPozadi.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PohybPozadi : MonoBehaviour
|
||||
{
|
||||
public float speed = 200f; // vyšší hodnoty, UI používá pixely
|
||||
private RectTransform rectTransform;
|
||||
|
||||
void Start()
|
||||
{
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
rectTransform.anchoredPosition += Vector2.down * speed * Time.deltaTime;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/VerticalBird/PohybPozadi.cs.meta
Normal file
2
Assets/Scripts/VerticalBird/PohybPozadi.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 90e623fc22c61b548b281ac008fe72cc
|
||||
21
Assets/Scripts/VerticalBird/PohybPozadiStop.cs
Normal file
21
Assets/Scripts/VerticalBird/PohybPozadiStop.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class MoveDown : MonoBehaviour
|
||||
{
|
||||
public float speed = 200f;
|
||||
public float stopY = -6315.304f;
|
||||
private RectTransform rectTransform;
|
||||
|
||||
void Start()
|
||||
{
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (rectTransform.anchoredPosition.y >= stopY)
|
||||
{
|
||||
rectTransform.anchoredPosition += Vector2.down * speed * Time.deltaTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/VerticalBird/PohybPozadiStop.cs.meta
Normal file
2
Assets/Scripts/VerticalBird/PohybPozadiStop.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51526c84dc764ca4fa78019d6ff5eca3
|
||||
@@ -5,7 +5,7 @@ public class Obstacle : MonoBehaviour
|
||||
{
|
||||
public float fallSpeed = 200f;
|
||||
public RectTransform robot;
|
||||
public float hitDistance = 60f;
|
||||
public float shrink = 20f;
|
||||
public string nextScene = "GameOver";
|
||||
|
||||
private RectTransform rectTransform;
|
||||
@@ -23,19 +23,29 @@ public class Obstacle : MonoBehaviour
|
||||
// Pád dolù
|
||||
rectTransform.anchoredPosition += Vector2.down * fallSpeed * Time.deltaTime;
|
||||
|
||||
// Detekce dotyku
|
||||
float distance = Vector2.Distance(
|
||||
rectTransform.anchoredPosition,
|
||||
robot.anchoredPosition
|
||||
);
|
||||
// Detekce dotyku pøes Rect
|
||||
Rect obstacleRect = GetWorldRect(rectTransform, shrink);
|
||||
Rect robotRect = GetWorldRect(robot, shrink);
|
||||
|
||||
if (distance < hitDistance)
|
||||
if (obstacleRect.Overlaps(robotRect))
|
||||
{
|
||||
isHit = true;
|
||||
Invoke("LoadNextScene", 0.5f);
|
||||
}
|
||||
}
|
||||
|
||||
Rect GetWorldRect(RectTransform rt, float shrinkAmount = 0f)
|
||||
{
|
||||
Vector3[] corners = new Vector3[4];
|
||||
rt.GetWorldCorners(corners);
|
||||
return new Rect(
|
||||
corners[0].x + shrinkAmount,
|
||||
corners[0].y + shrinkAmount,
|
||||
corners[2].x - corners[0].x - shrinkAmount * 2,
|
||||
corners[2].y - corners[0].y - shrinkAmount * 2
|
||||
);
|
||||
}
|
||||
|
||||
void LoadNextScene()
|
||||
{
|
||||
SceneManager.LoadScene(nextScene);
|
||||
|
||||
Reference in New Issue
Block a user