using UnityEngine;
using UnityEngine.UI;
public class PlayerMove : MonoBehaviour
{
public Body x;
public PageChange y;
public GameObject circle;
public Camera cam;
public int seconds;
private float timer;
private float percent;
private bool DoOnce;
private Vector2 difference;
private Vector2 start;
private void Start()
{
DoOnce = false;
}
public void Move(Vector2 target)
{
if (DoOnce == false)
{
start = transform.position;
Vector2 circlepos = circle.transform.position;
difference = target - circlepos;
DoOnce = true;
}
else if (timer <= seconds)
{
timer += Time.deltaTime;
percent = timer / seconds;
transform.position = start + (difference * percent);
}
else
{
timer = 0f;
x.timer = 0f;
DoOnce = false;
y.moving = false;
}
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Timers;
public class Body : MonoBehaviour
{
private RectTransform rt;
public PageChange x;
public PlayerMove y;
public Warning warning;
private float Distance;
private float AngleRad;
private float AngleDeg;
Vector2 target;
public Camera cam;
public GameObject circle;
public GameObject body;
public float timer;
private float percent;
private int temp;
private readonly Timer _MouseSingleClickTimer = new Timer();
void Start()
{
_MouseSingleClickTimer.Interval = 400;
_MouseSingleClickTimer.Elapsed += SingleClick;
}
private void Update()
{
TurnAndStretch();
if (x.moving) { y.Move(target); }
}
private void SetSeconds(float distance)
{
temp = (int)(distance / 500);
if (temp <= 1) { y.seconds = 1; }
else { y.seconds = temp; }
}
void SingleClick(object o, System.EventArgs e)
{
_MouseSingleClickTimer.Stop();
System.Diagnostics.Debug.WriteLine("Single Click");
}
private void TurnAndStretch()
{
if (Input.GetMouseButtonDown(0))
{
if (_MouseSingleClickTimer.Enabled == false)
{
_MouseSingleClickTimer.Start();
return;
}
else
{
_MouseSingleClickTimer.Stop();
if (x.moving == false)
{
Vector3 mousePos = Input.mousePosition + new Vector3(0, 0, 4f);
Vector2 bodypos = circle.transform.position;
Debug.LogFormat("Mouse Position {0} \nBody position {1}", mousePos, bodypos);
Distance = Vector2.Distance(bodypos, mousePos);
SetSeconds(Distance);
z.energyexpect = z.energy - (int)(Distance / 25);
if (z.energyexpect >= 0)
{
AngleRad = Mathf.Atan2(mousePos.y - bodypos.y, mousePos.x - bodypos.x);
AngleDeg = ((180 / Mathf.PI) * AngleRad) - 90;
transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
target = (Vector2)mousePos;
x.moving = true;
}
else
{
warning.WarningSet("You dont have enough energy.");
}
}
}
}
}
}
Im trying to make an object turn to a double mouse click position and move there but there is always something wrong either with the mouse click position or UI Image RectTransform position. Help me please