// This is a basic implementation of a touch-based player controller in Unity // The player can be moved by touching the screen and dragging to a new location
using UnityEngine; using System.Collections;
public class PlayerController1 : MonoBehaviour {
public GameObject circlePrefab; // Prefab for the circle that appears under the player when touched
private GameObject currentCircle; // The currently spawned circle (if any)
private Vector3 targetPosition; // The position the player is moving towards
private bool facingRight = true; // Whether or not the player is facing right
Coroutine currentCoroutine;
void Update()
{
if (Input.touchCount > 0) // Check if there are any touch events happening
{
Touch touch = Input.GetTouch(0); // Get information about the first touch event
switch (touch.phase)
{
case TouchPhase.Began:
if (IsTouchedOnPlayer(touch.position)) // If touched on player, spawn a circle under them and return without doing anything else
{
currentCircle = Instantiate(circlePrefab, transform.position, Quaternion.identity);
return;
}
break;
case TouchPhase.Moved:
if (currentCircle != null) // If there's already a circle spawned, move it to follow the finger movement
{
Vector3 touchPos = Camera.main.ScreenToWorldPoint(touch.position); // Convert screen coordinates to world coordinates
touchPos.z = 0;
currentCircle.transform.position = touchPos;
}
break;
case TouchPhase.Ended:
case TouchPhase.Canceled:
if (currentCircle != null)
Destroy(currentCircle);
HandleTouchEndedOrCanceled(touch); // Handle end/cancelled touches by moving the player towards their final destination
break;
}
}
}
private bool IsTouchedOnPlayer(Vector2 screenPosition)
{
Vector2 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
Collider2D[] colliders = GetComponents<Collider2D>();
foreach (var collider in colliders)
{
if (collider.OverlapPoint(worldPosition))
{
return true;
}
}
return false;
}
void HandleTouchEndedOrCanceled(Touch touch)
{
if (!IsTouchedOnPlayer(touch.position)) // If not touching the player at the end, set the target position to where they lifted their finger
{
Vector2 targetPos = Camera.main.ScreenToWorldPoint(touch.position);
targetPosition = targetPos;
if (currentCoroutine != null) // If already moving, stop the current coroutine so a new one can be started
StopCoroutine(currentCoroutine);
currentCoroutine = StartCoroutine(MoveToTarget()); // Start coroutine to move towards target position
}
}
IEnumerator MoveToTarget()
{
float moveSpeed = 5f; // Movement speed of 5 meters per second
float bufferDistance = 0.1f; // Buffer distance of 0.1 meters before stopping movement
Vector3 targetDirection;
while (Vector3.Distance(transform.position, targetPosition) > bufferDistance) // Keep moving towards the target until within buffer distance
{
if (transform.position == targetPosition) // If already at the destination, exit coroutine early
yield break;
targetDirection = (targetPosition - transform.position).normalized; // Calculate direction to move in
if (targetDirection.x > 0 && !facingRight) // Flip sprite if needed to face direction of movement
{
Flip();
}
else if (targetDirection.x < 0 && facingRight)
{
Flip();
}
if (Vector3.Distance(transform.position, targetPosition) < moveSpeed * Time.deltaTime) // If close enough to target that one more step would overshoot it, just set position to the target and exit
{
transform.position = targetPosition;
yield break;
}
transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime); // Move towards the target
yield return null; // Wait for next frame before continuing loop
}
}
void Flip() // Flips the sprite horizontally (useful when changing direction)
{
facingRight = !facingRight;
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
}
}