62 lines
1.7 KiB
C#
62 lines
1.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class RobotMovement : MonoBehaviour
|
|
{
|
|
public float moveDistance = 100f; // pixely, ne metry!
|
|
public float moveSpeed = 500f;
|
|
|
|
private RectTransform rectTransform;
|
|
private Vector2 targetPosition;
|
|
private bool isMoving = false;
|
|
|
|
void Start()
|
|
{
|
|
rectTransform = GetComponent<RectTransform>();
|
|
targetPosition = rectTransform.anchoredPosition;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (rectTransform.anchoredPosition.x > 450f)
|
|
{
|
|
rectTransform.anchoredPosition = new Vector2(450f, rectTransform.anchoredPosition.y);
|
|
Debug.Log("maximální pozice");
|
|
}
|
|
|
|
if (rectTransform.anchoredPosition.x < -450f)
|
|
{
|
|
rectTransform.anchoredPosition = new Vector2(-450f, rectTransform.anchoredPosition.y);
|
|
Debug.Log("maximální pozice");
|
|
}
|
|
|
|
if (isMoving)
|
|
{
|
|
rectTransform.anchoredPosition = Vector2.MoveTowards(
|
|
rectTransform.anchoredPosition,
|
|
targetPosition,
|
|
moveSpeed * Time.deltaTime
|
|
);
|
|
|
|
if (Vector2.Distance(rectTransform.anchoredPosition, targetPosition) < 0.5f)
|
|
{
|
|
rectTransform.anchoredPosition = targetPosition;
|
|
isMoving = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void MoveRight()
|
|
{
|
|
targetPosition = rectTransform.anchoredPosition + Vector2.right * moveDistance;
|
|
isMoving = true;
|
|
Debug.Log("pohyb do P");
|
|
}
|
|
|
|
public void MoveLeft()
|
|
{
|
|
targetPosition = rectTransform.anchoredPosition + Vector2.left * moveDistance;
|
|
isMoving = true;
|
|
Debug.Log("pohyb do L");
|
|
}
|
|
} |