How To Lock Direction While Jumping ?
Hello, i want to lock direction while my Character is jumping but i dont know how, i've tried a lot of ideas but i don't resolve my problem, so i decided to come here and ask u for help.
My code looks that:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] Transform playerCamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] float walkSpeed = 6.0f;
[SerializeField] float sprintSpeed = 9.0f;
[SerializeField] float moveSpeed;
[SerializeField] float gravity = -13.0f;
[SerializeField] float jumpHeight = 2.0f;
[SerializeField] [Range(0.0f, 0.5f)] float defmoveSmoothTime = 0.15f;
[SerializeField] [Range(0.0f, 0.5f)] float mouseSmoothTime = 0.03f;
[SerializeField] [Range(0.0f, 2.0f)] float jumpSmoothTime = 0.5f;
[SerializeField] bool lockCursor = true;
float moveSmoothTime = 0.3f;
float cameraPitch = 0.0f;
float velocityY = 0.0f;
private Vector3 moveVector;
CharacterController controller = null;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
void Start()
{
controller = GetComponent<CharacterController>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Update()
{
UpdateMouseLook();
UpdateMovement();
}
void UpdateMouseLook()
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}
void UpdateMovement()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (isGrounded && velocityY < 0)
{
velocityY = -2f;
}
if (controller.isGrounded)
{
velocityY = 0.0f;
moveSmoothTime = defmoveSmoothTime;
}
else
{
moveSmoothTime = jumpSmoothTime;
}
// Jump
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocityY = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocityY += gravity * Time.deltaTime;
// Movement WASD
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * moveSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
// Sprint
if (controller.isGrounded)
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = sprintSpeed;
}
else
{
moveSpeed = walkSpeed;
}
}
}
Comment
I want my character to fall in the direction it started jumping from, ignoring the camera movement, but now the character is moving in the direction following camera movement
Answer by MathNerd1O1 · Nov 22, 2020 at 11:01 PM
Only get the target direction if the player is on the ground. When you leave the ground, the target direction will stay the same until you hit the ground again.