Make a smooth rotation over time.
This is for a 2d game, I want to click on a tower, and then click again to move the cannon in the direction of the click. The code does that, but its instantaneous. I hace try lerp but dosent seem to get it right. This is the Code.
////////////////////////////////////////////////////////////////////////////
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ClickToRotate : MonoBehaviour { Vector3 targetPosition;
 bool esperando = false;
 public GameObject ToRotate;
 [Header("Ability UI Stuff")]
 public Button Ability1;
 public float AbilityCoolDownStart = 3;
 private float AbilityCoolDown;
 public GameObject RallyPoint;
 public GameObject AbilityMouseFollow;
 public enum FacingDirection
 {
     UP = 270,
     DOWN = 90,
     LEFT = 180,
     RIGHT = 0
 }
   
 
 public Quaternion FaceObject(Vector2 startingPosition, Vector2 targetPosition, FacingDirection facing)
 {
     Vector2 direction = targetPosition - startingPosition;
     float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
     angle -= (float)facing;
     return Quaternion.AngleAxis(angle, Vector3.forward);
 }
 // Update is called once per frame
 void Update ()
 {
     if (esperando == true && Input.GetKeyDown(KeyCode.Mouse0))
     {
         AbilityMouseFollow.SetActive(false);
         esperando = false;
         AbilityCoolDown = AbilityCoolDownStart;
         targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         targetPosition.z = transform.position.z;
         if (RallyPoint)
         {
             RallyPoint.transform.position = targetPosition;
             RallyPoint.SetActive(true);
             Debug.Log(targetPosition);
         }
       
         //Debug.Log("objectivo " + objetivo);
         //StartCoroutine(shoot());
         var dir = targetPosition - transform.position;
         var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         ToRotate.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         //ToRotate.transform.rotation = Quaternion.Slerp(transform.localRotation, Quaternion.AngleAxis(angle, Vector3.forward), Time.deltaTime * 3f);
     }
     if ((esperando == true && (Input.GetKeyDown(KeyCode.Mouse1))))
     {
         AbilityMouseFollow.SetActive(false);
        esperando = false;
     }
     
 }
 public void MoveRallyPoint()
 {
     AbilityMouseFollow.SetActive(true);
     //  Debug.Log("esperando " + objetivo);
     esperando = true;
 }
 
               }
Your answer
 
             Follow this Question
Related Questions
2D Enemy Script Rotate to look at player. 0 Answers
Walking around a sprite 1 Answer
[Solved] Problem with achieving rotation relative to current acceleration value 0 Answers
Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion 2 Answers
Unity 2D lookat and shoot issues 0 Answers