Simple First Person Controller

Hello! I'm doing my first project in unity with zero experience in coding and such thing, so please be patient with me if i dont use the right lingo.

For my university major I decided to build a section of a village because I am a modeller and I thought that it would be boring to just render out a camera sequence, so I came across unity and decided that it would be awesome for people to be able to walk around my city. That should be easy enough??? Ha! anyway.

So now my level is finished and I am ready to bake out my app BUT I've spent all this time getting the joystick/controller to work and now I've realised that the one I hand in to my lecturer for marking has to use just mouse and keyboard. I've used the FP controller prefab and set it to mouse and keyboard but it seems that the axis that front back and sides is set to is the world and not the camera. So when I press play everything is ok, but if i look around and/or turn around, none of the keys do what they are meant to.

ie:forwards goes left, backwards goes right and so on and so forth.

How do I set it up so the movement are relative to where the camera is looking?

ADDITIONAL

Hi thanks for that. Thing is I have really no idea what i'm doing. I'm just using the prefab with the script as is. I understand what you are saying but i don't know how to implement that into the FPSWalker script...

this is the prefab walker script:

var speed = 6.0;

var jumpSpeed = 8.0;

var gravity = 20.0;

TransformDirection(Vector3.right);

private var moveDirection = Vector3.zero;

private var grounded : boolean = false;

function FixedUpdate() { if (grounded) { // We are grounded, so recalculate movedirection directly from axes moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;

    if (Input.GetButton ("Jump")) {
        moveDirection.y = jumpSpeed;
    }
}

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;

}

@script RequireComponent(CharacterController)

I am yet to learn how to script, sorry really I'm really new :(

What you want is to use relative directions instead of the fixed ones... A good controller comes with the 3rd Person Platformer tutorial. You can check it out and see how they do it...

But basically the code you're probably looking for is:

var v3RelativeCameraRight: Vector3 = tCamera.TransformDirection(Vector3.right);

What this does is return a Vector3 that is the global direction that points right from the camera. If you would change Vector3.right to Vector3.forward you would get the direction the camera is looking at... etc.

This way you can move the character relative to your camera (Assuming you used a smooth-follow camera). If you want to show some of your code, it will be easier to point you in the right direction that way.

Edit:

It seems the FPSWalker script already does this, so no need to implement it after all :) You can erase that line you added. Your problem as I understand it now is that your hierarchy is set up incorrectly (although I don't know how this can happen, since it should have worked exactly the same on a controller and on the keyboard+mouse combo). If you want to try a quick and dirty solution - try changing the line:

moveDirection = transform.TransformDirection(moveDirection);

into

moveDirection = Camera.main.transform.TransformDirection(moveDirection);

If you want to try editing your hierarchy instead of this quick fix (or it won't work): If you look in your hierarchy there's a script in there that should control the rotation - it's probably MouseOrbit, and you're probably have two of them there. The one that is set up to rotate on the X axis is the one that needs to be on the top of the hierarchy. The idea is - It rotates the game object with the character controller. The camera is parented to it, and rotates on the X axis as well. Another MouseOrbit script is on the camera - and rotates it on the Y axis.

Right now - it's PROBABLY set up incorrectly, and if you look at it while playing - the camera rotates on the X axis, but the game object with the character controller is not rotated with it. Thus every time you press right - the direction is relative to the character controller, and so it's always the same since the character controller doesn't rotate.

If this is not the case or you're not sure what I mean - don't hesitate to post back...