Top down controller

Hi, so i have been trying to make an top down controller script. But i cant really figure out how to script it, i just want a simple walk around script using the arrow keys and i want it to be using a rigid body.

If anyone have an example script i can take a look at to see how i can write it or if you know where you can find one. It would be really helpful.

Thanks.

a little mnodifcation of my controller, enjoy.

[/code]

//top down player controller.
//A D strafe left right
//W &S foward back
//Q E rotateSpeed

//if player shouldn’t rotate remove rotateSpeed var, and Q&E controls. Change all Self refs to World, this is added so player can rotate and move foward relavtive to themselves.

//player up/down ,side/side speed
var speed = 10;
//player rotate speed
var rotateSpeed = 20;

//player movement
function Update () {

//moves player left right up down
var x = Input.GetAxis(“Horizontal”) * Time.smoothDeltaTime * speed;
var y = Input.GetAxis(“Vertical”) * Time.smoothDeltaTime * speed;

//W S Foward/Backward
if(Input.GetKey(KeyCode.W))
{
transform.Translate(0,0,speed * Time.deltaTime,Space.Self);
//Debug.Log(“Does Wwork?”);
}

if(Input.GetKey(KeyCode.S))
{
transform.Translate(0,0,-speed * Time.deltaTime,Space.Self);
//Debug.Log(“Does Swork?”);
}

//A D side to side
if(Input.GetKey(KeyCode.A))
{
transform.Translate(-speed * Time.deltaTime,0,0,Space.Self);
//Debug.Log(“Does A work?”);
}

if(Input.GetKey(KeyCode.D))
{
transform.Translate(speed * Time.deltaTime,0,0,Space.Self);
//Debug.Log(“Does D work?”);
}

//Q and E keys rotate:

if(Input.GetKey(KeyCode.Q))
{
transform.Rotate(0,-rotateSpeed * Time.deltaTime,0,Space.Self);
//Debug.Log(“Does Q work?”);
}

if(Input.GetKey(KeyCode.E))
{
transform.Rotate(0,rotateSpeed * Time.deltaTime,0,Space.Self);
//Debug.Log(“Does E work?”);
}

}

@script RequireComponent(CharacterController)

[/code]

This is a bit out of my understanding, i just want a very simple rigidbody controller to start things off with. I’m going to get into Character Controller and more advanced stuff later.
So you dont have any rigidbody controllers lying around that i can take a look at?

Well, just make a cube, slap this on it and it’ll add the character controller. Go in game and you cube will go where you want.

You could add a rigid body too.

you can delete the character component line and it wont make one. I tried it with a rigid body too, works fine.

It might not be the best way to script it but it works just fine. (I’m n00b too)

I don’t know how much simpler you can get though, Each button tells it to move a certian way.

ie:

if(Input.GetKey(KeyCode.W))
{
transform.Translate(0,0,speed * Time.deltaTime,Space.Self);
//Debug.Log(“Does Wwork?”);
}

If you push W, it translates (moves in straight line) in the players local (Self) space of the z plane (looking foward) (0,0,blah blah,Space) = (x,y,z, Space)
So it moves in the z direction at the speed of 10 (with time smooth for fast/slow computers)

speed * Time.deltaTime is in the z field (x,y,z)

On S I put -speed (which is backwards)

Space can be World (ie: W will always go positive z in world coords)
Self just means W goes positive z in players coords (only needed with the Q E rotation)

var speed = 10, lets you set the speed in editor, so if it’s too slow try 20 (in editor)

So i tried using this script, it works fine when it moves around but it’s walking in the wrong directions. None of the buttons is in the right direction.
You know what might be wrong?

(Having the player looking to the right in a -90 angle by the way)


Just fixed it, thanks alot for the help :slight_smile:

Thanks for explaining this, had some trouble understanding some parts until you explained it

No prob.

If it’s moving in wrong directions it’s probably your export. Max has a z up coord system, Unity is Y up, so your character could be looking the wrong way to start (should be looking to positive z when created).

either fix on export, change all the speed settings to -speed and vice versa or just make a new gameObject, put script on it, make you character a child of it in the hiearchy.

right, been testing your script out. Why are

in the code?
If i remove them nothing happends.

And can i do so the character turns in the direction you are walking without messing with the script?

Umm, actually I’m not sure. I did a tut, then modified the script alot to make it control differently. I thought it stopped working when I took that out, but now that I check again I see it doesn’t matter.

Changing the Space.Self to Space.World will make the controls work based on world coords (ie:W will always move character North). You should remove the rotation buttons if you do that, otherwise the player could walk at weird angles.

With Self the character will move forward according to which way it faces.

A bit out of date, but

are for the arrow-keys. Input.GetKey(KeyCode.W) etc. was for WASD, and “Horiz” and “Vert” are for the arrow keys. (1 being up and -1 being down, etc.)