I am trying to make smooth plane movement that follows the cursor.
This is the code that does so:
void Update()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 0;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + rotationOffset));
Vector3 targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPos.z = 0;
transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
if (transform.position == targetPos)
{
Die();
}
}
public void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("hit");
}
This works well by its self but I have run into an issue where it doesn't detect collision. Ive never used Vector3.MoveTowards with movement before but I'm guessing that's the problem. If anyone had any suggestions it would be very appreciated.