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 instead of 3d space?
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.
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.
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... .
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 );
}
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 instead of 3d space?
– Meltdown