- Home /
Player Movement based on x/y screen coordinates, not world coordinates
I'm trying to develop a top-down shooter, however I'm having some trouble figuring out how to control player movement based on x/y screen coordinates instead of world coordinates. While I have most of the movement worked out, I can't figure out how to apply the 2D movement in 3D space - I've looked into the functions to do this in Unity, and while I understand them in theory the examples provided, as well as a few days of research, hasn't yielded an answer I can understand or break down - maybe there are better resources out there, but I can't find them.
Please help!
I think you are going about this the wrong way. Unity3D is built for making/manipulating objects in 3D space, is there any particular reason why you want to do movement based on screen co-ordinates ins$$anonymous$$d of 3d space?
$$anonymous$$eltdown: Imagine a game like SmashTV, where the position of the mouse controls the character's rotation (eg always facing the mouse), but "forward" should always be up on screen +x, down on screen -x, left on -y and right on +y.
Thanks for the tips everyone, still working on figuring it out however.
Answer by loopyllama · Mar 20, 2011 at 10:52 AM
You can use the x y of the screen. When you figure out your object's movement in screen space then you can convert it to world space and move your character in worldspace.
Camera.ScreenToWorldPoint
http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToWorldPoint.html
When doing this you must set the z properly. It is the distance of your camera to your object in worldspace. If you fail to set the z properly you will get an incorrect result.
Answer by Owen-Reynolds · Mar 20, 2011 at 04:29 PM
The trick is to think of what you want it to look like, then how to do it. It sounds like you want the player to always be facing "North," in a top view.
A really cheesy way to do that is to child the camera to the player, aim it down (maybe x rotation of 70, for a little look ahead) and move it until you get a good field of view.
Later, if you're feeling ambitious, add a script that does the same thing, but now you can program some bounce, a scroll wheel, screen shake... .
Answer by Robo_Chiz · Sep 01, 2012 at 06:19 PM
I had trouble with this. I created a small cuboid and and positioned it inside the player. I Gave it the cuboid a Character Controller. Then I gave it the script at the bottom of this answer, and then parented the Player Object to the Cuboid. The Cuboid should handle the movement and the player should follow it because it is a child. (Side Note: You may need to rotate the cuboid to get the move in the right direction. )
private var moveDirection : Vector3 = Vector3 .zero ;
var speed : float = 5;
function Update ()
{
var controller : CharacterController = GetComponent (CharacterController );
moveDirection = transform.TransformDirection (Input .GetAxis ("Horizontal"), 0, Input .GetAxis ("Vertical"));
moveDirection *= speed ;
controller .Move (moveDirection * Time .deltaTime );
}