Hi. I wrote a simple script for my 2d topdown character to look in mouse direction.
here is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateToCursor : MonoBehaviour {
Vector3 mousePos;
Camera cam;
Rigidbody2D rid;
void Start () {
rid = this.GetComponent<Rigidbody2D> ();
cam = Camera.main;
}
void Update () {
rotateToCamera ();
}
void rotateToCamera () {
mousePos = cam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z - cam.transform.position.z));
rid.transform.eulerAngles = new Vector3 (0, 0, Mathf.Atan2 ((mousePos.y - transform.position.y), (mousePos.x - transform.position.x)) * Mathf.Rad2Deg);
}
}
its okay but i want my character to rotate after mouse slower.
I dont want to change constant rotation speed but max rotation that character can perform.
I want it to be like that:
If i move cursor slowly he will follow it with normal rotation speed, but when i move cursor fast he will be limited to certain rotation speed and follow cursor in that max speed.
I tried everything to make it work but i cant manage to make it happen.
Can u guys give me at least a hint ? Maybe whole script is wrong and i should do it using Quaternion.LookRotation and Quaternion.RotateTowards ? Thanks in advance and sorry for my english.