Question by
ronman121111 · Aug 01, 2021 at 02:35 PM ·
player movementmovment
moving while jumping increasing the gravity(?)
when the player jumps and stays his place(not using WASD) the gravity working fine. when the player is jumping and using the WASD keys, the gravity becomes higher and I don't know how to fix it ,can someone help me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] Transform playerCamera;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] bool lockCursor = true;
[SerializeField] float walkSpeed = 6f;
[SerializeField] float gravity = -13f;
[SerializeField] [Range(0.0f, 0.5f)] float movesmoothTime = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float mousesmoothTime = 0.03f;
[SerializeField] private AnimationCurve jumpFallOff;
[SerializeField] private float jumpMultiplier;
[SerializeField] private KeyCode jumpKey;
[SerializeField] float slopeForce;
[SerializeField] float slopeForceRayLength;
private bool isJumping;
float cameraPitch = 0.0f;
float velocityY = 0f;
CharacterController controller;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
if(lockCursor == true)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
// Update is called once per frame
void Update()
{
Mouselook();
UpdateMovment();
}
void Mouselook()
{
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 UpdateMovment()
{
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, mousesmoothTime);
if (controller.isGrounded)
{
velocityY = 0f;
}
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
if (Input.GetAxisRaw("Vertical") != 0 || Input.GetAxisRaw("Horizontal") != 0 && OnSlope())
controller.Move(Vector3.down * controller.height / 2 * slopeForce * Time.deltaTime);
JumpInput();
}
private bool OnSlope()
{
if (isJumping)
return false;
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, controller.height / 2 * slopeForceRayLength))
if (hit.normal != Vector3.up)
return true;
return false;
}
private void JumpInput()
{
if (Input.GetKeyDown(jumpKey) && !isJumping)
{
isJumping = true;
StartCoroutine(JumpEvent());
}
}
private IEnumerator JumpEvent()
{
controller.slopeLimit = 90f;
float timeInAir = 0.0f;
do
{
float jumpForce = jumpFallOff.Evaluate(timeInAir);
controller.Move(Vector3.up* jumpForce * jumpMultiplier * Time.deltaTime);
timeInAir += Time.deltaTime;
yield return null;
} while (!controller.isGrounded && controller.collisionFlags != CollisionFlags.Above);
controller.slopeLimit = 90f;
isJumping = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Problem: trigger within object 1 Answer
How do I make a charakter with immovable parts? 1 Answer
Smooth Rotation on WASD keys pressed? 1 Answer
Player Movement Goes Crazy When Hit On Side Of Moving Platform 0 Answers
Third Person controller 1 Answer