- Home /
Top Down Shooter Aiming
Why would this not cause my player to rotate towards the mouse, assuming the camera is looking down on the player. Exactly vertical.
var currentGun : GameObject;
function Start () {
}
function Update ()
{
var position = Input.mousePosition;
var newPosition = Vector3(position.x,position.y,camera.main.transform.position.y- currentGun.transform.position.y);
var lastPosition = Camera.main.ScreenToWorldPoint(newPosition);
transform.LookAt(lastPosition);
}
There is no way to deter$$anonymous$$e what is happening with this code since we don't have any idea what the position of currentGun is. This is nearly identical to your last post. As mentioned in your last post, use Camera.main.nearClipPlane for the Z value the Vector you pass to ScreenToWorldPoint() and your code should work.
Then you likely have issues not related to this code. Run the sample code I posted in the other post and watch where the block are placed. $$anonymous$$ake sure the "transform" above is really set to the character you want to look at the camera.
Thanks for that, let me explain my set-up. I have taken the fps controller, removed mouse look and reposition camera above player. The above script is attached to the graphics of the controller, which is not affected by any other scripts. I want the roation and movement to be independent of eachother, this is why I have done this. $$anonymous$$y gun should rotate towards the mouse(it is attached to the graphics) but not rotate up or down which is why i want the z to be level with the gun.
Then you calculate the position in world space first using nearClipPlane. After you have the point in world space, then you change the z coordinate to match z coordinate of your character (I think it's Z. Similar problems Tank turrets and guns reset the Y when looking down the z axis).
Answer by $$anonymous$$ · Feb 07, 2013 at 11:09 AM
Because your player's orientation is not correct. Make sure your character's forward vector is really the forward you want, so it looks in the direction of the z axis (blue arrow).
What's your problem then? I copy-pasted your script on a cube, made another cube child of that cube and used that as "currentGun", and it works. You're not telling us if your character does NOT rotate at all, or it rotates but not how you want?!
oh right, sorry for trying to get some info.. Place Debug.Log()
statements in your code so you see what values are computed or if you even have reference to all the objects you're using. Your problem is probably not with this script, since it works for me. Debugging is your friend.
Ok, so I've just found that newPosition is stuck at 0,0,0 even though position is working fine. Any ideas?
Answer by OmarAlhaddad · Feb 14, 2013 at 01:10 PM
Dont use LookAt for anything not totally 3D, calculate your rotation angle manually instead.
there are many ways, here is one (might need to tweak it depending on your setup):
float angle = (Mathf.Atan2 (target.y, target.x) * Mathf.Rad2Deg) -90f;
Goodluck