Search Unity

How to make 2D object rotate toward Mouse

Discussion in 'Getting Started' started by unitytachpro, Jun 23, 2021.

  1. unitytachpro

    unitytachpro

    Joined:
    Jun 19, 2021
    Posts:
    5
    paste this in fixed update method
    Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

    Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);

    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);

    LIKE PLS
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,445
  3. unitytachpro

    unitytachpro

    Joined:
    Jun 19, 2021
    Posts:
    5
    Tip: you can replace Update with FixedUpdate and it will be more acurate