Jumping Function in my Player Controller Script isn't working
For some reason, the jumping function in my script isn't working. I have no errors, it simply just isn't activating. What am I doing wrong here?
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public float walkSpeed = 2;
public float runSpeed = 6;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
public float force;
public Rigidbody rb;
public bool grounded;
Animator animator;
Transform cameraT;
void Start () {
force = 500;
rb = gameObject.GetComponent<Rigidbody>();
grounded = true;
animator = GetComponent<Animator> ();
cameraT = Camera.main.transform;
}
void FixedUpdate () {
if (Input.GetKeyDown(KeyCode.Space))
{
if (grounded)
{
grounded = false;
rb.AddForce(gameObject.transform.up * force);
}
}
if (grounded)
{
grounded = false;
rb.AddForce(gameObject.transform.up * force);
}
Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
Vector2 inputDir = input.normalized;
if (inputDir != Vector2.zero) {
float targetRotation = Mathf.Atan2 (inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
}
bool running = Input.GetKey (KeyCode.LeftShift);
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp (currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
transform.Translate (transform.forward * currentSpeed * Time.deltaTime, Space.World);
float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
animator.SetFloat ("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}
void OnCollisionEnter(Collision collidingObject)
{
grounded = true;
}
}
Answer by airbagnr1 · Apr 03, 2018 at 07:55 PM
Might be because you are setting grounded false before you apply the jump force, try putting the grounded = false after it and as Cornelius said, you also don’t need that part. :) Try putting debug statements in to see if the code is being reached!
Okay so I removed “grounded = false;” and added a debut statement, and it is reaching the command, it’s just not activating the jump action... Any ideas?
It doesn’t look like there is an issue with the code as far as i can tell. I’m guessing the grounded is so that the player doesn’t constantly jump? If so, a better way might be to use and overlapsphere. Additionally i would try setting the force to some extremely large value to see if it is working maybe your gameobject is just too heavy. Also did you put the debug line after the rb.addforce.. line? If so put a break point on the rb.addforce so that you can read the values in visual studio(if you are using visual stuido)
Okay so I found a better jump script that is actually making the character jump, BUT he is jumping too fast. Is there a way to slow it down?
public class Jump2 : $$anonymous$$onoBehaviour { public float speed = 1.0f; public float gravity = -10.0f;
private float verticalVelocity;
public float jumpForce = 50.0f;
public float gravityJump = 14.0f;
private CharacterController _charController;
void Start() {
_charController = GetComponent<CharacterController>();
}
void Update() {
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
movement = Vector3.Clamp$$anonymous$$agnitude(movement, speed);
movement.y = gravity;
movement *= Time.deltaTime;
movement = transform.TransformDirection(movement);
_charController.$$anonymous$$ove(movement);
if (_charController.isGrounded) {
verticalVelocity = -gravityJump * Time.deltaTime;
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
verticalVelocity = jumpForce;
}
} else {
verticalVelocity -= gravityJump * Time.deltaTime;
}
Vector3 jumpVector = new Vector3(0, verticalVelocity, 0);
_charController.$$anonymous$$ove (jumpVector * Time.deltaTime);
}
}
Answer by Cornelis-de-Jager · Apr 03, 2018 at 03:42 AM
Remove the second jump command
if (Input.GetKeyDown(KeyCode.Space))
{
if (grounded)
{
grounded = false;
rb.AddForce(gameObject.transform.up * force);
}
}
// Delete the section below
if (grounded)
{
grounded = false;
rb.AddForce(gameObject.transform.up * force);
}