Repaired hole minigame
This commit is contained in:
@@ -1,46 +1,55 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
[RequireComponent(typeof(Collider))]
|
||||
public class DraggableObject : MonoBehaviour
|
||||
{
|
||||
[Header("Přetahování")]
|
||||
public float dragSmoothness = 15f;
|
||||
|
||||
[Header("Vizuální zpětná vazba")]
|
||||
public SpriteRenderer spriteRenderer;
|
||||
public Renderer Renderer;
|
||||
public Color normalColor = Color.white;
|
||||
public Color dragColor = new Color(1f, 1f, 0.5f);
|
||||
public float scaleOnDrag = 1.15f;
|
||||
|
||||
private Rigidbody2D rb;
|
||||
private Rigidbody rb;
|
||||
private Camera mainCamera;
|
||||
private bool isDragging = false;
|
||||
private Vector3 targetPosition;
|
||||
private Vector3 originalScale;
|
||||
private bool hasBeenScored = false;
|
||||
private Plane _dragPlane;
|
||||
private Collider col;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
rb = GetComponent<Rigidbody>();
|
||||
col = GetComponent<Collider>();
|
||||
mainCamera = Camera.main;
|
||||
originalScale = transform.localScale;
|
||||
if (spriteRenderer == null)
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
if (Renderer == null)
|
||||
Renderer = GetComponent<Renderer>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
rb.gravityScale = 0f;
|
||||
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
|
||||
rb.useGravity = false;
|
||||
rb.constraints = RigidbodyConstraints.FreezeRotation;
|
||||
targetPosition = transform.position;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
HandleInput();
|
||||
}
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (isDragging)
|
||||
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * dragSmoothness);
|
||||
{
|
||||
Vector3 newPos = Vector3.Lerp(rb.position, targetPosition, Time.fixedDeltaTime * dragSmoothness);
|
||||
rb.MovePosition(newPos);
|
||||
}
|
||||
}
|
||||
|
||||
void HandleInput()
|
||||
@@ -48,13 +57,13 @@ public class DraggableObject : MonoBehaviour
|
||||
if (Input.touchCount > 0)
|
||||
{
|
||||
Touch touch = Input.GetTouch(0);
|
||||
Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f));
|
||||
//Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f));
|
||||
|
||||
if (touch.phase == TouchPhase.Began)
|
||||
TryStartDrag(worldPos);
|
||||
TryStartDrag(touch.position);
|
||||
else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
|
||||
{
|
||||
if (isDragging) targetPosition = worldPos;
|
||||
if (isDragging) UpdateDrag(touch.position);
|
||||
}
|
||||
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
|
||||
{
|
||||
@@ -63,49 +72,70 @@ public class DraggableObject : MonoBehaviour
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
|
||||
//Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
TryStartDrag(worldPos);
|
||||
TryStartDrag(Input.mousePosition);
|
||||
else if (Input.GetMouseButton(0) && isDragging)
|
||||
targetPosition = worldPos;
|
||||
UpdateDrag(Input.mousePosition);
|
||||
else if (Input.GetMouseButtonUp(0) && isDragging)
|
||||
EndDrag();
|
||||
}
|
||||
}
|
||||
|
||||
void TryStartDrag(Vector3 worldPos)
|
||||
void TryStartDrag(Vector3 screenPos)
|
||||
{
|
||||
if (GetComponent<Collider2D>().OverlapPoint(worldPos))
|
||||
StartDrag(worldPos);
|
||||
}
|
||||
Debug.Log("Trying to start drag at: " + screenPos);
|
||||
Ray ray = mainCamera.ScreenPointToRay(screenPos);
|
||||
RaycastHit hit;
|
||||
|
||||
void StartDrag(Vector3 worldPos)
|
||||
{
|
||||
isDragging = true;
|
||||
rb.linearVelocity = Vector2.zero;
|
||||
targetPosition = worldPos;
|
||||
transform.localScale = originalScale * scaleOnDrag;
|
||||
if (spriteRenderer != null)
|
||||
if (Physics.Raycast(ray, out hit))
|
||||
{
|
||||
spriteRenderer.color = dragColor;
|
||||
spriteRenderer.sortingOrder = 10;
|
||||
if (hit.collider == col)
|
||||
{
|
||||
StartDrag(hit.point);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StartDrag(Vector3 hitPoint)
|
||||
{
|
||||
Debug.Log("Started dragging at: " + hitPoint);
|
||||
isDragging = true;
|
||||
rb.isKinematic = false;
|
||||
rb.linearVelocity = Vector3.zero;
|
||||
_dragPlane = new Plane(-mainCamera.transform.forward, hitPoint);
|
||||
targetPosition = hitPoint;
|
||||
transform.localScale = originalScale * scaleOnDrag;
|
||||
if (Renderer != null)
|
||||
{
|
||||
Renderer.material.color = dragColor;
|
||||
}
|
||||
}
|
||||
void UpdateDrag(Vector3 screenPos)
|
||||
{
|
||||
Ray ray = mainCamera.ScreenPointToRay(screenPos);
|
||||
float distance;
|
||||
if (_dragPlane.Raycast(ray, out distance))
|
||||
{
|
||||
targetPosition = ray.GetPoint(distance);
|
||||
}
|
||||
}
|
||||
void EndDrag()
|
||||
{
|
||||
Debug.Log("Ended dragging");
|
||||
isDragging = false;
|
||||
rb.isKinematic = true;
|
||||
transform.localScale = originalScale;
|
||||
if (spriteRenderer != null)
|
||||
if (Renderer != null)
|
||||
{
|
||||
spriteRenderer.color = normalColor;
|
||||
spriteRenderer.sortingOrder = 0;
|
||||
Renderer.material.color = normalColor;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnScored()
|
||||
{
|
||||
Debug.Log("Object scored");
|
||||
if (hasBeenScored) return;
|
||||
hasBeenScored = true;
|
||||
isDragging = false;
|
||||
@@ -123,8 +153,8 @@ public class DraggableObject : MonoBehaviour
|
||||
elapsed += Time.deltaTime;
|
||||
float t = elapsed / duration;
|
||||
transform.localScale = Vector3.Lerp(startScale, Vector3.zero, t);
|
||||
if (spriteRenderer != null)
|
||||
spriteRenderer.color = new Color(normalColor.r, normalColor.g, normalColor.b, 1f - t);
|
||||
if (Renderer != null)
|
||||
Renderer.material.color = new Color(normalColor.r, normalColor.g, normalColor.b, 1f - t);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user