How come I can't jump?,How Come my script won;t actually let my character jump???
Here's the script... for some reason, it won't let the player jump...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
public float moveSpeed = 5.0f;
public float jumpForce = 10.0f;
private float moveFB, moveLR;
private float verticalVelocity;
private float gravity = 12.0f;
private CharacterController player;
private bool hasJumped;
// Use this for initialization
void Start ()
{
player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update ()
{
Movement ();
if (Input.GetButtonDown ("Jump"))
{
hasJumped = true;
}
ApplyGravity();
}
void Movement()
{
//Getting inputs from WASD keys
moveFB = Input.GetAxis ("Vertical") * moveSpeed;
moveLR = Input.GetAxis ("Horizontal") * moveSpeed;
//Control the movement of the character
Vector3 movement = new Vector3(moveLR, verticalVelocity, moveFB);
//Fix the orientation of the character
movement = transform.rotation * movement;
//Moving the player
player.Move(movement * Time.deltaTime);
}
private void Jump()
{
//verticalVelocity = jumpForce;
hasJumped = true;
}
private void ApplyGravity()
{
if (player.isGrounded == true)
{
if (hasJumped == false) {
verticalVelocity = Physics.gravity.y;
}
else
{
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity = Physics.gravity.y;
hasJumped = false;
}
}
}
,Here's the script... for some reason, it won't jump
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
public float moveSpeed = 5.0f;
public float jumpForce = 10.0f;
private float moveFB, moveLR;
private float verticalVelocity;
private float gravity = 12.0f;
private CharacterController player;
private bool hasJumped;
// Use this for initialization
void Start ()
{
player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update ()
{
Movement ();
if (Input.GetButtonDown ("Jump"))
{
hasJumped = true;
}
ApplyGravity();
}
void Movement()
{
//Getting inputs from WASD keys
moveFB = Input.GetAxis ("Vertical") * moveSpeed;
moveLR = Input.GetAxis ("Horizontal") * moveSpeed;
//Control the movement of the character
Vector3 movement = new Vector3(moveLR, verticalVelocity, moveFB);
//Fix the orientation of the character
movement = transform.rotation * movement;
//Moving the player
player.Move(movement * Time.deltaTime);
}
private void Jump()
{
//verticalVelocity = jumpForce;
hasJumped = true;
}
private void ApplyGravity()
{
if (player.isGrounded == true)
{
if (hasJumped == false) {
verticalVelocity = Physics.gravity.y;
}
else
{
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity = Physics.gravity.y;
hasJumped = false;
}
}
}
Answer by JoeDarido · Mar 02, 2018 at 06:17 AM
The bool hasJumped is becoming true the moment you click space, Before the jump triggers, so you'll never jump, try setting it to true after you give Physics.gravity.y
to the verticalVelocity
and your problem is solved
I understand what you're saying, but I don't know how I would do that... Where exactly would I have to move it to?
Could I just uncomment it?
add it to the brackets of this if: if(hasjumped==false)
or to one of its elses or to all the elses of that if and this if
Your answer
Follow this Question
Related Questions
how to jump in parabola. 0 Answers
Jump not working in my Player Movement Script 1 Answer
Change the "jumping speed" in the FirstPersonController by using another script 0 Answers
Jumping Problems 0 Answers