- Home /
i have a mouse script and i want the script to work with my right joystick. Please help.
i have this script using which i can make my player to look in the direction of the mouse cursor. i want the same functionality for my right joystick. like if i move my joystick up the player should look in positive z direction and so forth. please help me.
using UnityEngine; using System.Collections;
public class LookTowardMouse : MonoBehaviour {
void Update ()
{
//Get the Screen positions of the object
Vector2 positionOnScreen = Camera.main.WorldToViewportPoint (transform.position);
//Get the Screen position of the mouse
Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
//Get the angle between the points
float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen)+90;
transform.rotation = Quaternion.Euler (new Vector3(0f, -angle, 0f));
}
float AngleBetweenTwoPoints(Vector3 a, Vector3 b) {
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
}
}
Comment
Your answer