Question by
unity_mm9M4nB6AUHabg · Nov 08, 2020 at 08:28 PM ·
c#jumpingisgroundedsprinting
How to get isGrounded to work
So I am trying to be able to jump but with my ground detector, I cant!
Here is my movement script it also has other thing like sprinting(which also doesnt work for the same reason) I am using an empty gameobject as the GroundDetector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Com.Kawaiisun.TRANESON
{
public class Movement : MonoBehaviour
{
//public
public float Speed;
public float sprintModifier;
public float JumpForce;
public Camera PlayerCam;
public Transform GroudDetector;
public LayerMask Ground;
//private
private Rigidbody rig;
private float BaseFOV;
private float SprintFOVModifier = 1.5f;
private void Start()
{
BaseFOV = PlayerCam.fieldOfView;
Camera.main.enabled = false;
rig = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float t_hmove = Input.GetAxisRaw("Horizontal");
float t_vmove = Input.GetAxisRaw("Vertical");
bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool jump = Input.GetKeyDown(KeyCode.Space);
bool isGrounded = Physics.Raycast(GroudDetector.position, Vector3.down, 0.1f, Ground);
bool isJumping = jump && isGrounded;
bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
if (isJumping)
{
rig.AddForce(Vector3.up * JumpForce);
}
Vector3 t_direction = new Vector3(t_hmove, 0, t_vmove);
t_direction.Normalize();
float t_adjustedSpeed = Speed;
if (isSprinting) t_adjustedSpeed *= sprintModifier;
Vector3 t_targetVelocity = transform.TransformDirection(t_direction * t_adjustedSpeed * Time.deltaTime);
t_targetVelocity.y = rig.velocity.y;
rig.velocity = t_targetVelocity;
if (isSprinting) { PlayerCam.fieldOfView = Mathf.Lerp(PlayerCam.fieldOfView, BaseFOV * SprintFOVModifier, Time.deltaTime * 8f); }
else { PlayerCam.fieldOfView = Mathf.Lerp(PlayerCam.fieldOfView, BaseFOV, Time.deltaTime * 8f); ; }
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613015412im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D-Platformer wrong jumps 0 Answers
How to double jump? i just can jump once when it touch the ground.. help me guys =D 1 Answer
Hey can someone help me with my camera script ? 0 Answers
Bugs on move in air(falling); 0 Answers
When i jump in my FPS the player quickly teleports up and down. How let i jump smoothly? 0 Answers