- Home /
First person controller, no gravity?
Is it possible to somehow set a "no gravity" mode to the first person controller? To get a airplane/fly simulation camera?
Im still new to unity, so this might be a very simple thing, but i cant find it so far.
thanx!
Answer by aldonaletto · Mar 14, 2012 at 12:55 PM
If you're using the First Person Controller that comes with Unity, you must modify the variable movement.gravity inside CharacterMotor.js (the script that controls the character movement). Place this code in a script attached to the character, and you can set the gravity value calling the function ChangeGravity(g):
function ChangeGravity(g: float){ var chMotor: CharacterMotor = GetComponent(CharacterMotor); chMotor.movement.gravity = g; }If you want to start the game with a lunar gravity, for instance:
function Start(){ ChangeGravity(1.6); // set a lunar gravity at start }The standard value is 10.0; if you set to 0.0, you will have no gravity at all.
but in that case im still not able to move in the Z direction. it only allows me to go into X and Y somehow :/
Well, if there's no gravity, you won't fall down, however you still won't move upwards unless you add that too.
@Dantevw1986, you're messing axes: you can move in X and Z, but not in Y (unless you jump or fall).
If you want to control vertical movement, it's better to write your own movement script, attach it to the character and disable the script Character$$anonymous$$otor. Since you don't want gravity, things are somewhat easier - for instance:
var speedH = 5.0; var speedV = 10.0;
function Update(){ var move: Vector3; move.x = Input.GetAxis("Horizontal") speedH Time.deltaTime; move.z = Input.GetAxis("Vertical") speedH Time.deltaTime; move.y = Input.GetAxis("$$anonymous$$ouse ScrollWheel") speedV Time.deltaTime; move = transform.TransformDirection(move); GetComponent(CharacterController).$$anonymous$$ove(move); } Attach this script to the character and disable Character$$anonymous$$otor, and you will be able to move up and down with the scroll wheel, as well as move around with the familiar AWSD keys. You will also be able to aim the camera to any direction with the mouse, because the character already have two scripts $$anonymous$$ouseLook that control the camera direction.
what i have now, is a 3rd person controller, where the camera is attached to the mouse. Person walks in the direction of where the camera is facing to. So if i just hold down the W key for forward, the Character moves forward, but when i move the mouse to left and right, the movement direction changes, without using the A and D keys.
this is the same that i want, only now add the option to look up and down too. this does not work, even if gravity is 0 now.
any ideas on this one, or do you need the scripts i use now for that ?
Answer by Obliviate · Sep 26, 2012 at 11:29 PM
I have a script that I came across, I changed a couple of things but it works and allows me to go up and down with no problems. The camera follows but I am still having trouble smoothing it out.. Hit me up on Skype if you want to take a look at it.. My Skype username is Lightning4771 upon add request tell me why your adding me.. :)
Your answer

Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Camera Rotate behind rotating ball 1 Answer
Billboard without Update loop 0 Answers
how to find mouse look 1 Answer
How to take video input to unity ? 1 Answer