I have my sprite turn and move to where i click on the screen, but i want my sprite to first rotate in the direction on my mouse then start moving toward where i clicked but instead it’s both rotating and moving at the same time.
public float speedX = 100;
public float speedY = 100;
public Rigidbody2D rb;
public Vector2 destination;
public Vector2 place;
public Vector2 journey;
public Vector2 gap;
public float speed = 50;
public float angle;
public int angle2;
public bool rotate = false;
void Start()
{
rb = GetComponent<Rigidbody2D>();
place = rb.transform.position;
}
void Update()
{
Rotation();
Movement();
angle2 = (int)angle;
}
void LateUpdate()
{
place = rb.transform.position;
gap.x = Mathf.Abs(destination.x) - Mathf.Abs(place.x);
gap.y = Mathf.Abs(destination.y) - Mathf.Abs(place.y);
if (.05 > Mathf.Abs(gap.x))
if (.05 > Mathf.Abs(gap.y))
rb.velocity = new Vector2(0, 0);
}
void Rotation()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 0f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
angle = angle - 90;
}
if (Input.GetMouseButtonDown(1))
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 0f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
angle = angle - 90;
}
Quaternion qTo = Quaternion.Euler(new Vector3(0, 0, angle2));
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
}
void Movement()
{
if (Input.GetMouseButtonDown(0))
{
destination = Camera.main.ScreenToWorldPoint(Input.mousePosition);
journey.x = destination.x - place.x;
journey.y = destination.y - place.y;
rb.velocity = new Vector2(journey.normalized.x * speedX * Time.deltaTime, journey.normalized.y * speedY * Time.deltaTime);
}
}