Hi. I have a gameobject which always looks at the mouse. It changes its local scale depending on its angle.
Script :
var mouse_pos : Vector3;
var target : Transform;
var object_pos : Vector3;
var angle : float;
var Min : float;
var Max :float;
var Man : GameObject;
var FacingL : boolean;
function Start () {
}
function Update () {
mouse_pos = Input.mousePosition;
mouse_pos.z = 5.23;
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
if(FacingL == true) {
angle = Mathf.Atan2(-mouse_pos.y, -mouse_pos.x) * -Mathf.Rad2Deg;
}
if(FacingL == false) {
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
}
//angle = Mathf.Clamp(angle, Min ,Max);
transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
Debug.Log(angle);
if(angle > 92 && FacingL == false) {
Man.transform.localScale.x *=-1;
FacingL = true;
}
if(angle > 92 && FacingL == true) {
Man.transform.localScale.x *=-1;
FacingL = false;
}
}
When FacingL is true eveything is fine. But when FacingL is false, I have to move my mouse very slowly for script to react. When I move my mouse fast and the angle is more than 92 and facingL is false, it doesnt react.For example angle is 120, facingL is false, and it doesnt do anything when i hold the mouse at that position. It only changes its local scale when i move my mouse slowly. It is a 2d game. I removed the part where FacingL is true and the facingL is false part worked fine. Those two parts :
if(angle > 92 && FacingL == false) {
Man.transform.localScale.x *=-1;
FacingL = true;
}
if(angle > 92 && FacingL == true) {
Man.transform.localScale.x *=-1;
FacingL = false;
}
Those part dont work with each other. What should I do?