Sooo let's say your making a top down shooter, you want your player to look in the direction of your mouse like so:
(Yeah I know, not the best picture)
Andddd here's the script to do so. I got it from here.
To use this script you need to attach it to the camera you use, and then manually set your fpc variable as the object you want to look at mouse. Keep in mind the camera needs to be looking straight down on the game object to work the best.
To use this script you need to attach it to the camera you use, and then manually set your fpc variable as the object you want to look at mouse. Keep in mind the camera needs to be looking straight down on the game object to work the best.
private var worldPos : Vector3;
private var mouseX : int;
private var mouseY : int;
private var cameraDif : int;
var fpc : GameObject;
function Start () {
cameraDif = camera.transform.position.y - fpc.transform.position.y;
}
function Update () {
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));
var turretLookDirection:Vector3 = Vector3(worldPos.x,fpc.transform.position.y, worldPos.z);
fpc.transform.LookAt(turretLookDirection);
}
Explanation:
var fpc : GameObject;
var fpc : GameObject;
This is the game object we are going to make face our mouse.
function start is called once after the everything is loaded and the awake function is called. It's called before the Update function. Read more here.
cameraDif = camera.transform.position.y - fpc.transform.position.y;
It's to make sure your object only rotates along rotation.x when the LookAt function is used.
mouseX = Input.mousePosition.x;
worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));
So were setting a vector3 position by using the camera.ScreenToWorldPoint. This converts screen space to world space. Read more here.
var turretLookDirection:Vector3 = Vector3(worldPos.x, fpc.transform.position.y, worldPos.z);
A new Vector 3 is made. This uses the game Object you are rotating y position.
fpc.transform.LookAt(turretLookDirection);
The LookAt function rotates the transform towards the vector 3 you set. Read more here. Since you are using the fpc's y transform position in the turretLookDirection, the turrent never rotates up or down.
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
These are integers that are used in the camera.ScreenToWorldPoint function to determine where the mouse is. Coordinates start 0 at bottom left corner of the screen. Read more here.
These are integers that are used in the camera.ScreenToWorldPoint function to determine where the mouse is. Coordinates start 0 at bottom left corner of the screen. Read more here.
worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));
So were setting a vector3 position by using the camera.ScreenToWorldPoint. This converts screen space to world space. Read more here.
var turretLookDirection:Vector3 = Vector3(worldPos.x, fpc.transform.position.y, worldPos.z);
A new Vector 3 is made. This uses the game Object you are rotating y position.
fpc.transform.LookAt(turretLookDirection);
The LookAt function rotates the transform towards the vector 3 you set. Read more here. Since you are using the fpc's y transform position in the turretLookDirection, the turrent never rotates up or down.
Got the script here.
Did I do a good job explaining this? Do you have questions or mistakes you'd like to correct? Just comment below.
Did I do a good job explaining this? Do you have questions or mistakes you'd like to correct? Just comment below.
This helped a lot. Thank You.
ReplyDelete