126 lines
4.1 KiB
C#
126 lines
4.1 KiB
C#
|
using UnityEngine;
|
||
|
|
||
|
public class Paddle : MonoBehaviour
|
||
|
{
|
||
|
public float moveSpeed = 10f;
|
||
|
public float boundaryOffset = 0.5f;
|
||
|
public float launchInfluence = 0.7f; // How much paddle movement affects ball launch
|
||
|
public float bounceInfluence = 1.2f; // How much paddle movement affects bounce
|
||
|
|
||
|
private float screenHalfWidth;
|
||
|
private float paddleHalfWidth;
|
||
|
private Camera mainCamera;
|
||
|
private Vector3 lastPosition;
|
||
|
private Vector2 currentVelocity;
|
||
|
private bool isTouching = false;
|
||
|
private float touchStartX;
|
||
|
private float paddleStartX;
|
||
|
private bool isMobile;
|
||
|
|
||
|
void Start()
|
||
|
{
|
||
|
mainCamera = Camera.main;
|
||
|
paddleHalfWidth = transform.localScale.x / 2f;
|
||
|
screenHalfWidth = mainCamera.aspect * mainCamera.orthographicSize;
|
||
|
lastPosition = transform.position;
|
||
|
|
||
|
// Определяем, запущено ли на мобильном устройстве
|
||
|
isMobile = Application.isMobilePlatform;
|
||
|
}
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
if (isMobile)
|
||
|
{
|
||
|
HandleTouchInput();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
HandleDesktopInput();
|
||
|
}
|
||
|
|
||
|
// Calculate current velocity
|
||
|
currentVelocity = (transform.position - lastPosition) / Time.deltaTime;
|
||
|
lastPosition = transform.position;
|
||
|
}
|
||
|
|
||
|
private void HandleTouchInput()
|
||
|
{
|
||
|
if (Input.touchCount > 0)
|
||
|
{
|
||
|
Touch touch = Input.GetTouch(0);
|
||
|
|
||
|
switch (touch.phase)
|
||
|
{
|
||
|
case TouchPhase.Began:
|
||
|
isTouching = true;
|
||
|
touchStartX = touch.position.x;
|
||
|
paddleStartX = transform.position.x;
|
||
|
break;
|
||
|
|
||
|
case TouchPhase.Moved:
|
||
|
case TouchPhase.Stationary:
|
||
|
if (isTouching)
|
||
|
{
|
||
|
// Вычисляем смещение в пикселях
|
||
|
float touchDelta = touch.position.x - touchStartX;
|
||
|
|
||
|
// Конвертируем в мировые координаты
|
||
|
float worldDelta = (touchDelta / Screen.width) * (screenHalfWidth * 2);
|
||
|
|
||
|
// Вычисляем новую позицию
|
||
|
float newX = Mathf.Clamp(paddleStartX + worldDelta,
|
||
|
-screenHalfWidth + paddleHalfWidth + boundaryOffset,
|
||
|
screenHalfWidth - paddleHalfWidth - boundaryOffset);
|
||
|
|
||
|
// Плавно перемещаем к целевой позиции
|
||
|
float currentX = transform.position.x;
|
||
|
newX = Mathf.MoveTowards(currentX, newX, moveSpeed * Time.deltaTime);
|
||
|
|
||
|
// Применяем позицию
|
||
|
transform.position = new Vector3(newX, transform.position.y, transform.position.z);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TouchPhase.Ended:
|
||
|
case TouchPhase.Canceled:
|
||
|
isTouching = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void HandleDesktopInput()
|
||
|
{
|
||
|
// Get input
|
||
|
float moveInput = Input.GetAxis("Horizontal");
|
||
|
|
||
|
// Calculate new position
|
||
|
Vector3 pos = transform.position;
|
||
|
pos.x += moveInput * moveSpeed * Time.deltaTime;
|
||
|
|
||
|
// Clamp position to screen bounds
|
||
|
float clampedX = Mathf.Clamp(pos.x,
|
||
|
-screenHalfWidth + paddleHalfWidth + boundaryOffset,
|
||
|
screenHalfWidth - paddleHalfWidth - boundaryOffset);
|
||
|
|
||
|
// Update position
|
||
|
transform.position = new Vector3(clampedX, pos.y, pos.z);
|
||
|
}
|
||
|
|
||
|
public Vector2 GetCurrentVelocity()
|
||
|
{
|
||
|
return currentVelocity;
|
||
|
}
|
||
|
|
||
|
public Vector2 GetLaunchVelocity()
|
||
|
{
|
||
|
return currentVelocity * launchInfluence;
|
||
|
}
|
||
|
|
||
|
public Vector2 GetBounceVelocity()
|
||
|
{
|
||
|
return currentVelocity * bounceInfluence;
|
||
|
}
|
||
|
}
|