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;
}
}