Question by
Sanderdrack · May 22 at 05:19 AM ·
rigidbodyscripting beginnermovement scriptjumping
Cant jump while sprinting (RigidBody Movement)
Hi! I use rigidBody movement but I can't figure out why I can't jump while sprinting
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
int iSprint = 0;
public float moveSpeed;
public float sprintSpeed;
public float groundDrag;
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump;
[Header("KeyBinds")]
public KeyCode jumpKey = KeyCode.Space;
public KeyCode sprintKey = KeyCode.LeftShift;
[Header("Ground Check")]
public float playerHeight;
public LayerMask WhatisGround;
public bool grounded;
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
Rigidbody rb;
public Transform Tay;
public Camera myCamera;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
ResetJump();
}
void Update()
{
Vector3 ray = Tay.TransformDirection(Vector3.down) * playerHeight * 0.5f;
grounded = Physics.Raycast(Tay.position, Vector3.down, playerHeight * 0.5f + 0.1f, WhatisGround);
Debug.DrawRay(Tay.position, ray, Color.cyan);
Sprint();
MyInput();
SpeedControl();
if (grounded)
rb.drag = groundDrag;
else
rb.drag = 0;
}
private void FixedUpdate()
{
MovePlayer();
}
private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
if (Input.GetKey(jumpKey) && readyToJump && grounded)
{
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
}
private void MovePlayer()
{
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
if (grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
else if (!grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
}
private void SpeedControl()
{
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
if (flatVel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
}
private void Jump()
{
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
readyToJump = true;
}
IEnumerator FovPlus()
{
for (int i = 0; i < 3; i++)
{
myCamera.fieldOfView = myCamera.fieldOfView + 5;
yield return new WaitForSeconds(0.02f);
}
}
IEnumerator FovMinus()
{
for (int i = 0; i < 3; i++)
{
myCamera.fieldOfView = myCamera.fieldOfView - 5;
yield return new WaitForSeconds(0.02f);
}
}
private void Sprint()
{
if (Input.GetKey(jumpKey) && readyToJump && grounded)
{
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
if (Input.GetKey(sprintKey))
{
moveSpeed = sprintSpeed;
if (iSprint == 0)
{
Debug.Log("fov +");
iSprint++;
StartCoroutine(FovPlus());
}
}
else
{
moveSpeed = 2.5f;
if (iSprint == 1)
{
Debug.Log("fov -");
iSprint--;
StartCoroutine(FovMinus());
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to block one input while another is in use? 1 Answer
Jumping disables my horizontal movement. 0 Answers
Help with making a endles runner 0 Answers