Персонаж двигается по клику, но возникает проблема на коротких дистанциях - не срабатывает
Flip();
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public float speed;
private Vector3 targetPosition;
private bool isMoving = false;
private bool facingRight = true;
void Update()
{
if(Input.GetMouseButtonDown(0))
{
SetTargetPosition();
}
if(isMoving)
{
Move();
}
if(!facingRight && targetPosition.x > 0.1)
{
Flip();
}
else if(facingRight && targetPosition.x < 0.1)
{
Flip();
}
}
void SetTargetPosition()
{
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPosition.z = transform.position.z;
isMoving = true;
}
void Move()
{
//transform.rotation = Quaternion.LookRotation(Vector3.forward, targetPosition);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
if(transform.position == targetPosition)
{
isMoving = false;
}
}
private void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}