- Home /
Set up a 3D fly through
I want to set up a simple navigation game which has a first person fly through in space. The existing scripts for first person navigation only allow 2D motion in a terrian (turn left and right, go forward and backward) controlled by arrow keys and mouse. Is there any ways (existing scritps with some modification) for me to do it?
It would be helpful if you posted exactly how you want to move. Do you want your character to be affected by gravity? Or do you just want movement with the arrowkeys and a mouselook that lets you fly around?
Answer by TheMPC · Apr 21, 2010 at 09:14 AM
I think you are looking for the following:
Add the "Mouse Look" component to your camera.
Change the MaximumY to 90 and the minimum to -90 (this will allow you to look up and down)
Now add the FPSWalker Component to your Camera which will allow you to move around.
Change the script of the FPSWalker, change it from:
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;
}
TO:
function FixedUpdate() { // Calculate the move direction moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
controller.Move(moveDirection * Time.deltaTime);
}
This will enable you to move freely in 3D space by the arrow keys or the WASD keys.
Answer by Dominic · Apr 21, 2010 at 03:03 PM
Thank you very much for the answer. The scripts work well.
But as I press the arrow keys, the character goes forward, backward, left and right. Can I go up and down also? How to do it?
Your answer
![](https://koobas.hobune.stream/wayback/20220612180238im_/https://answers.unity.com/themes/thub/images/avi.jpg)