Hi there,
I’ve finished the 2D course for Unity and now following the mobile game course. Basically I wanted to create a mobile version of my Laser Defender game. I’ve managed to add a health pickup system for the game with some help obviously
But now I am facing some issues with the new controller for my player. I am sharing my updated player class here for the mobile
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
[Header("General")]
[SerializeField] float moveSpeed = 5f;
[SerializeField] float touchThreshold = 50f;
[Header("Padding")]
[SerializeField] float paddingLeft;
[SerializeField] float paddingRight;
[SerializeField] float paddingTop;
[SerializeField] float paddingBottom;
Vector2 minBounds;
Vector2 maxBounds;
Shooter shooter;
private Vector2 touchStartPosition;
private bool isTouchMoving = false;
void Awake()
{
shooter = GetComponent<Shooter>();
}
void Start()
{
InitBounds();
}
void Update()
{
HandleInput();
Move();
}
void InitBounds()
{
Camera mainCamera = Camera.main;
minBounds = mainCamera.ViewportToWorldPoint(new Vector2(0, 0));
maxBounds = mainCamera.ViewportToWorldPoint(new Vector2(1, 1));
}
void HandleInput()
{
if (Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == UnityEngine.TouchPhase.Began) // Fully qualify the enum
{
if (IsTouchOnPlayer(touch.position))
{
touchStartPosition = touch.position;
isTouchMoving = true;
shooter.isFiring = true;
}
}
else if (touch.phase == UnityEngine.TouchPhase.Ended || touch.phase == UnityEngine.TouchPhase.Canceled) // Fully qualify the enum
{
isTouchMoving = false;
shooter.isFiring = false;
}
}
}
else
{
isTouchMoving = false;
shooter.isFiring = false;
}
}
bool IsTouchOnPlayer(Vector2 touchPosition)
{
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
float distance = Vector2.Distance(touchPosition, screenPosition);
return distance < touchThreshold;
}
void Move()
{
if (isTouchMoving)
{
Vector2 touchDelta = (Input.touches[0].position - touchStartPosition) * moveSpeed * Time.deltaTime;
MovePlayerWithDelta(touchDelta);
touchStartPosition = Input.touches[0].position;
}
}
void MovePlayerWithDelta(Vector2 delta)
{
Vector3 newPos = transform.position + (Vector3)delta; // Convert Vector2 to Vector3
newPos.x = Mathf.Clamp(newPos.x, minBounds.x + paddingLeft, maxBounds.x - paddingRight);
newPos.y = Mathf.Clamp(newPos.y, minBounds.y + paddingBottom, maxBounds.y - paddingTop);
transform.position = newPos;
}
public void Heal(int amount)
{
Health healthComponent = GetComponent<Health>();
if (healthComponent != null)
{
healthComponent.Heal(amount);
}
}
}
I have a windows pc and an iphone so I’ve downloaded the Unity Remote 5 app to test the game on my iphone but the issue I am facing is that with the current setup the only thing I could tweak is the touch threshold and even with bigger numbers for this value my player jumps all over the screen.
Could you please have a look at this script and share some wisdom with me? Is my approach is correct and achievable? Or should I add a joystick UI to the game? I am trying to achieve a more intuitive and user friendly gameplay for my game
Thanks in advance.