- Home /
player flicks when mouse is near the player
can anyone help me solve this problem
using UnityEngine; using System.Collections;
public class Aim : MonoBehaviour {
public GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint (transform.position);
if (dir.x <= 90) {
player.transform.localRotation = Quaternion.Euler (180,0,180);
} else {
player.transform.localRotation = Quaternion.Euler (0, 0, 0);
}
float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
}
}
Answer by Naphier · Oct 14, 2016 at 05:41 PM
You need to add a "dead zone" around the player. Something like this:
if (dir.x > 90 && dir.x < 100) return;
but your code has more problems. You should be converting Input.mousePosition to worldspace coordinates otherwise you're going to have very different values based on the screen size. This anser will help with that. http://answers.unity3d.com/questions/540888/converting-mouse-position-to-world-stationary-came.html
my games a 2d so wouldn't ScreenToWorldPoint make it difficult for me??
Nope. Its more of a problem that mouse position is in pixel coordinates and your screen pixels may change (different device sizes, different window sizes, etc). $$anonymous$$ouse position is almost always converted to a world position (unless in screen position GUI).
Your answer
![](https://koobas.hobune.stream/wayback/20220612094624im_/https://answers.unity.com/themes/thub/images/avi.jpg)