im beginner to unity so im using this code to rotate object with mouse with max rotation of 35 degrees,it is working but it's only possible to rotate object if mouse is under of that object. here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotObj : MonoBehaviour {
//private float baseAngle = 0.0f;
private float maxRotaion = 35f;
void Update () {
var dir = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
var angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
if (angle >= -maxRotaion && angle <= maxRotaion)
transform.localRotation = Quaternion.AngleAxis (angle, Vector3.forward);
}
}
as i think, localrotation is making that problem? how can i fix this without removing maximum rotation angle and will be able to rotate object not just from mouse under object, but from any position? my english grammar is very bad but i hope you will understand