- Home /
Free Movement (Space/Underwater) With First Person Controller
Hey guys, I have spent the past few weeks stumped trying every scrip and answer trying to get this to work. I am trying to get the first person controller to be able to move up and down along with the default sideways/forwards/backwards. Can anyone help me out? Thanks.
Well I would assume you would make a bool of being in the water, just like having a boolean for the isGrounded boolean. And then, if the player is inWater, set the gravity of the player to whatever you would wish. And also, if the player is inWater, and if the spacebar is held, add an upwards force, else if the player is inWater but spacebar is not held, apply a downwards force.
The problem with the First Person Controller is the lack of a rigidbody. I'm not an expert on the First Person Controller, but from what I understand, all of its physics are controlled by the scripts and barely by the world physics. Since it has no rigidbody, functions such as "rigidbody.AddForce()" cannot be used. $$anonymous$$y recommendation would be to make your own character controller that uses a rigidbody, that way you can use things such as the rigidbody.AddForce(). If you would like some help with this, I would be glad to assist you
Answer by chrisall76 · Apr 24, 2014 at 05:11 AM
You would have a first create a new controller based off of rigidbodies, as the regular one is weak for anything other than what it was made for (plus, coding your own is better always). As said, you should have a bool for if the player is underwater. If he is, his jump (using AddForce) with happen as long as your underwater. I'll help you as making your own controller feel nice takes a bit of testing.
(this is based off my own controller, unlike mine though this doesn't have a separate script for just taking in input)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private bool _Jump;
private bool _Crouch;
private bool Running = false;
private float Speed = 0;
[HideInInspector]
public bool losingStamina = false;
[HideInInspector]
public bool canJump = true;
[HideInInspector]
public bool isMoving = true;
[HideInInspector]
public bool isGrounded = true;
[HideInInspector]
public bool isUnderWater = true;
public float walkSpeed = 10;
public float runSpeed = 15;
public float AirModifier = 1.5f;
public float fakeFriction = 0.86f;
public float jumpForce = 40;
public float isGroundCheck = 1;
void Update () {
//Speed Modifier
if (Running == true && isMoving && isGrounded) {
Speed = runSpeed;
} else if (Running && isMoving && isGrounded) {
Speed = walkSpeed;
} else{
Speed = walkSpeed/AirModifier;
}
isGrounded = Physics.Raycast(transform.position, -Vector3.up, isGroundCheck);
Running = Input.GetKey(KeyCode.leftShift);
_Jump = Input.GetKeyDown(KeyCode.Space);
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if(h >= 0.1 || h <= -0.1 || v >= 0.1 || v <= -0.1 || _Jump){
isMoving = true;
}else{
isMoving = false;
}
//Limit Speed
if(rigidbody.velocity.magnitude > Speed){
rigidbody.velocity = rigidbody.velocity.normalized * Speed;
}
//Movement
this.rigidbody.velocity = new Vector3 (this.rigidbody.velocity.x * fakeFriction, this.rigidbody.velocity.y, this.rigidbody.velocity.z * fakeFriction);
Vector3 MovementForce = new Vector3 (move.x, 0, move.z);
MovementForce.Normalize ();
Vector3 MoveForce = transform.TransformDirection (MovementForce);
if (Moving) {
rigidbody.AddForce (MoveForce * Speed);
}
if (isGrounded && canJump && _Jump || isUnderWater) {
rigidbody.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
}
}
Your answer
