Hi, so i have been working a bit on a top down arena shooter.
I have a basic script that rotates the player towards the mouse and that follow the player when he moves around.
private var worldPos : Vector3;
private var mouseX : int;
private var mouseY : int;
private var cameraDif : int;
var playerObject : GameObject;
private var playerX : float;
private var playerZ : float;
function Start ()
{
cameraDif = camera.transform.position.y - playerObject.transform.position.y;
}
function Update ()
{
playerX = playerObject.transform.position.x;
playerZ = playerObject.transform.position.z;
transform.position = Vector3(playerX, 13, playerZ);
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));
playerObject.transform.LookAt(worldPos);
}
The script calculates the distance from the camera and then checks for the mouse position on the screen. The script then removes the distance from the player to the camera to have the player rotate in the right Y position and then makes the player rotate at the position where the mouse is.
The script is working fine except for one problem that i have.
When i mouse of the player he starts looking down as if the mouse is under the player for some reason. I think it might have something to do with the pivot point but i have tested around and did not find a sollution. Do anyone have any clue how to fix this?
Thanks for taking your time to read this post.
// Former Known As