- Home /
FPS velocity question
Hi yeah, beginners question here, Im working on a movement script and cannot figure out how to add velocity from jumping, what i mean is when i jump with the character, if i release the forward button he will stop mid air and fall directly down. I've searched everywhere on the net and the closest I've come is for a rigid body, as you can see here its for a Character controller. i came across addReletiveForce function but don't know hoe to implement it. script guide doesnt help. :P anybody got any ideas, i need them to continue in the trajectory he is fall even if the button is release, like real life. thanks for any help :D!!!
using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class StandardMovement : MonoBehaviour {
public float mouseSensitivity = 5.0f;
public float jumpSpeed = 20.0f;
public float upDownRange = 60.0f;
public float walkSpeed = 5.0f;
public float sprintSpeed = 10.0f;
float verticalVelocity = 0;
float verticalRotation = 0;
float actualSpeed;
public GameObject playerCamera;
CharacterController characterController;
// Use this for initialization
void Start () {
Screen.lockCursor = true; // Hides the cursor on playing
characterController = GetComponent<CharacterController> (); //Retrieves CC
}
// Update is called once per frame
void Update () {
// ** Movement of Player **
actualSpeed = walkSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime; //Applies acceleration p/s of Gravity when falling
// Restricts player from jumping twice in mid-air ir until player hits ground
if ( characterController.isGrounded && Input.GetButtonDown ("Jump")) {
verticalVelocity = jumpSpeed;
}
// Camera
float rotLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity;
transform.Rotate (0, rotLeftRight, 0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp (verticalRotation, -upDownRange, upDownRange);
playerCamera.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
//Allows sprinting
if (characterController.isGrounded && Input.GetKey(KeyCode.LeftShift)) {
actualSpeed = sprintSpeed;
}
if (characterController.isGrounded && Input.GetKeyUp(KeyCode.LeftShift)) {
actualSpeed = walkSpeed;
}
float forwardSpeed = Input.GetAxis ("Vertical") * actualSpeed; //Movement speed of player - forward movement
float sideSpeed = Input.GetAxis ("Horizontal") * actualSpeed / 2; //Movement speed of player - side movement
Vector3 speed = new Vector3 ( sideSpeed, verticalVelocity, forwardSpeed ); //Vector for movement of the player
speed = transform.rotation * speed; // ?
characterController.Move ( speed * Time.deltaTime ); //Applies speed of player p/frame
}
}
Your answer
Follow this Question
Related Questions
How can I stop the fps controller jumping up slopes? 1 Answer
Patching glitches 1 Answer
unity FPS controller besy JUMPING setting ? 1 Answer
How to Jump, Sprint and Sneak 0 Answers
Gravity Lift/Jump Pad for FPS game 1 Answer