- Home /
How can you make a FPS controller camera "fly"?
Hi there,
I'm trying to figure out how I can make my FPS controller go up or down (based on my mouse Y coordinates).
I'm using 0 gravity and it lets me roam around across the x and z axis, I just have no control over the Y axis.
EDIT: The part which determines the Y velocity when the camera isn't touching the ground looks like this:
private function ApplyGravityAndJumping (velocity : Vector3) {
if (!inputJump || !canControl) {
jumping.holdingJumpButton = false;
jumping.lastButtonDownTime = -100;
}
if (inputJump && jumping.lastButtonDownTime < 0 && canControl)
jumping.lastButtonDownTime = Time.time;
if (grounded)
velocity.y = Mathf.Min(0, velocity.y) - movement.gravity * Time.deltaTime;
else {
velocity.y = movement.velocity.y - movement.gravity * Time.deltaTime;
// When jumping up we don't apply gravity for some time when the user is holding the jump button.
// This gives more control over jump height by pressing the button longer.
if (jumping.jumping && jumping.holdingJumpButton) {
// Calculate the duration that the extra jump force should have effect.
// If we're still less than that duration after the jumping time, apply the force.
if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight)) {
// Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.
velocity += jumping.jumpDir * movement.gravity * Time.deltaTime;
}
}
// Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
velocity.y = Mathf.Max (velocity.y, -movement.maxFallSpeed);
}
if (grounded) {
// Jump only if the jump button was pressed down in the last 0.2 seconds.
// We use this check instead of checking if it's pressed down right now
// because players will often try to jump in the exact moment when hitting the ground after a jump
// and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,
// it's confusing and it feels like the game is buggy.
if (jumping.enabled && canControl && (Time.time - jumping.lastButtonDownTime < 0.2)) {
grounded = false;
jumping.jumping = true;
jumping.lastStartTime = Time.time;
jumping.lastButtonDownTime = -100;
jumping.holdingJumpButton = true;
// Calculate the jumping direction
if (TooSteep())
jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount);
else
jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount);
// Apply the jumping force to the velocity. Cancel any vertical velocity first.
velocity.y = 0;
velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight);
// Apply inertia from platform
if (movingPlatform.enabled &&
(movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
) {
movement.frameVelocity = movingPlatform.platformVelocity;
velocity += movingPlatform.platformVelocity;
}
SendMessage("OnJump", SendMessageOptions.DontRequireReceiver);
}
else {
jumping.holdingJumpButton = false;
}
}
return velocity;
}
That part deals with gravity and jumping. There are a couple of other code snipets which also deal with it... The script is almost 600 lines long and tonight is the first time I've even look at scripts sorry. :S Oh and also - I've disabled jumping, but am not sure if the code entails that because I'm not grounded initially, that I'm in a jump state. 3am so forgive me for not looking right now. :[
I've heard there is a fly through camera somewhere, but I've been looking for almost 4 hours now and I've had no luck. This is agonising. Any help would be duly appreciated please
post the fpsinput controller script and character motor script or whatever is controlling your character (prefereably just the bits controllling it. check updated post
post the section of script which controls gravity, it's not a big a deal as your making it out to be
Answer by AngryOldMan · Apr 28, 2011 at 01:18 PM
post the code which you are using to control at the moment. Have you added an axis and referenced that axis in code to control the y position of your object? I realise this isn't an answer but if I post a comment you will not be able to update me. I will edit my answer to be of use for you when I get more information :)
EDIT
Basically inordeer to get the character to move in 3 dimensions you need to find the section in the script which determines your character controllers move direction. Somewhere in the script will be a line which states how much movement to apply to the y axis every frame in order to simulate gravity. You can reduce that number to 0 and it will stop it from falling by default. Then you have to map an input axis in the input settings name it something like "Height" using this input axis in the script, assuming the section that moves reads movement via some form of
moveDirection = Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
add your input axis to the vector3 y axis there
moveDirection = Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Height"),Input.GetAxis("Vertical"));
If you have turned gravity to 0 in the physics manager this only effects rigidbodies and does not determine the gravity of a character controller component.
EDIT 2
right now that I realise what the code looks like what you need to do is take out all of that code, if you dont want any gravity and you dont need a jump then what you need to do is find the section in the scripts which calls/activates that function and delete it or comment it out. It occurs to me now that you would be better off using a rigidbody and adding force to it based on your inputs.
Unity is a heavily script based game engine and if you want it to really customise a game or make an individual one then you will have to learn to programme. Stealing scripts and using default scripts will only get you so far and it will never make your game unique.
Hi AngryOld$$anonymous$$an, I don't actually have any code at this moment except for a custom mouse look script which had implementations for Q and E left and right turning. Basically I have a DXF model, a few lights and then the $$anonymous$$ain Camera which has the standard (Custom)$$anonymous$$ouseLook, Character Controller, FPSInput Controller and Character $$anonymous$$otor scripts. I've set gravity to 0 too. Pretty much everything else is at default values currently :/
I think I get what you're saying. I found the part which takes care of vertical movement. It basically sets velocity.y to the appropriate value depending on what gravity is etc. It's starting to sound complex though, I'd have to use trigonometry in order to deter$$anonymous$$e how much of the horizontal velocity should be turned into vertical velocity? It almost sounds like I'd be ripping a fair amount of the guts out of the standard script.
Hi thanks for that. I'm a proficient programmer don't worry about that (being a grad student who's taken 3D papers done in C++). I'm not looking to make a game, I'm looking to make an interactive environment which can represent different layers of information on demand overlaid onto a 3D model. The reason why I'm looking for another script is because it would be quicker, that's one of the benefits of OO program$$anonymous$$g right? Anyhow, thanks for your help, I do believe that the camera controller includes or inherets a rigid body, I just don't have the time to look for where and how it's implemented
so after all that it was just a case of you wanted someone to provide you with a script. If you were so proficient with scripting you would realise that this kind of thing can be done in about ten $$anonymous$$utes from scratch. I smell B.S. or maybe is the strong stench of someone too lazy to bother (note it took you longer to not get an answer here than it would have to do it yourself from scratch)
I guess your username really does denote your attitude towards people. All I wanted was for someone to refer me to a script which I assumed (wrongly I apologize) that would exist somewhere. Flying cameras have been around for a long time, take the counterstrike death camera for example. I'm rushed for time at the moment and really don't have the capacity to edit a script which is 600 lines long, when the advice you're giving me is wrong. I wouldn't post here if it wasn't an emergency and I hadn't tried for at least a couple of hours. I don't know why you're getting hostile toward me. Thx tho..
Your answer
Follow this Question
Related Questions
Camera movement and gun follow in unity 1 Answer
Moving FPSCamera to fixed positions during OnClick of objects 0 Answers
How to make FPS Camera bounce when walking 1 Answer
how to clamp my FPS camera,How to do clamp my FPS camera 0 Answers
FPS: Make animated character weapon follow camera movement 1 Answer