Player Movement?

Im making a top down 2D game and i just need to know how to control a player and make it move.

Thanks :smile:

In Standard Assests/Prefabs there is a first person controller prefab you can probably modify for your needs.

Problem is, i’m really bad at this stuff, so how could i modify it for my needs?

:slight_smile:
I’m not really the techie type with this either, but you could delete the camera in the prefab and set a new camera over your scene and the player should move around with the WASD and arrow keys in the scene which you could watch from above where you have placed the new camera.

I’ll give it a shot, thanks :smile:

It didn’t work, when i move the mouse it moves it back and forth and as soon as i press one of the WASD keys it starts moving down slowly and doesn’t stop.

What is the “it” you are referring to, the camera or the player?

The player :wink:

A top down controller could look like this:

Create an empty game object and name it “character” or the like.
Add the following components:
Rigidbody with weight 50-100
Capsule collider with radius 0.4 and length 1.0 on the Y axis as direction.
A simple mesh representation (this is your character mesh)

(Alternatively to the below, just use the camera that is set up in the default scene)
Add a game object and name it “camera”. Move it so it is a child of the object above.
Add the following components:
Camera
Move the “camera” gameobject around so that it is looking down on top of the “character” object.

Create a script named “TopDownCharacterController” and add it to the “character” game object.

Open the “TopDownCharacterController” script and add some code to control the movement.

The control script might look like:

using UnityEngine;
public class TopDownCharacterController : MonoBehavkiour
{
    public transform Camera;
    public float MoveSpeed = 10f;
    private Vector3 _lastPosition;
    
    private void Start()
    {
        if(!Camera)
            Camera = Camera.main;
        _lastPosition = transform.position;
    }
    private void Update()
    {
        Vector3 movement = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis("Vertical"));
        transform.Translate(movement*Time.deltaTime*MoveSpeed);
        transform.rotation = Quaternion.LookRotation(transform.position - _lastPosition, Vector3.up);
        _lastPosition = transform.position;
    }
}

This will rotate the camera along with the character object, which is probably not what you want, and it doesn’t smooth any rotations, but it might get you started. Additionally, I’m at work, so I can’t test it, but it should do the trick :wink:

Happy coding
/Klum

I give up, it doesnt seem to work for me :cry:

I’m sticking to FPS games thanks to the trusty First person controller prefab :smile:

Have a look at this movie, the setup is as I mentioned above, using the FPC prefab, but moving the camera out of the prefab (or creating a new one) and placing it in position above the scene. I’m thinking the problem you are having is that you are losing orientation in your movement. Putting an object on the front of the cylinder will show you which way the object will move when you press the keys.

http://hometime.net/FPC_Overhead/FPC_Overhead.html

I’m not quite sure what i’m doing wrong but i give up :?

I know it is easy to get frustrated when you don’t understand how things are working and they seem to be working against you.
Maybe try again a little later when the inspiration again arises.
Or do some research on the game type to see if you can get some insight.
Was the example in the movie in the direction of what you were looking for?
I would post the scene but it’s using the version 3 b2 program so unless you have that version I don’t think it would be usable to you.