I`ve coded a simple FPS Character Controller movement script based on this code: Unity-FPS-Controller/PlayerController.cs at master · Acacia-Developer/Unity-FPS-Controller · GitHub
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float moveSmoothTime = 0.3f;
[SerializeField] float speed = 6.0f;
[SerializeField] float gravity = -9.81f;
[SerializeField] float jumpHeight = 3.0f;
float currentSpeed;
CharacterController controller;
Vector2 currentDir = Vector2.zero;
Vector2 currenDirVelocity = Vector2.zero;
Vector3 falling = Vector3.zero;
Vector3 velocity = Vector3.zero;
void Start()
{
controller = GetComponent<CharacterController>();
currentSpeed = speed;
}
void Update()
{
UpdateMovement();
}
void UpdateMovement()
{
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currenDirVelocity, moveSmoothTime);
falling.y += (gravity * Time.deltaTime);
if(controller.isGrounded)
{
falling.y = 0;
}
if(controller.isGrounded && Input.GetAxisRaw("Jump") == 1)
{
falling.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * currentSpeed;
controller.Move((velocity + falling) * Time.deltaTime);
}
}
It works great, but i cant make the air movement proper. I am using the script below to rotate the view, and MouseX rotates whole character instead of camera, so transform.forward vector in movement script move the character in the direction of view each frame the movement key is pressed, which is pretty good to use in first-person game in total, but causes troubles in air controlling. I need to move the character in air using world space vector (without *or* minimal changing of the direction for the entire time of jump) in same direction that player was moving before the Space key was pressed, and i don
t know how.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLook : MonoBehaviour
{
[SerializeField] Transform playerCamera;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] bool lockCursor = true;
[SerializeField] float mouseSmoothTime = 0.03f;
float cameraPitch;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
void Start()
{
if(lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
}
}
void Update()
{
UpdateMouseLook();
}
void UpdateMouseLook()
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
}
}
Any ideas how to make the air movement look like a trajectory of jump or make the air mobility less?