Need help with my RigidbodyFPSController.cs

Updated on November 9, 2017 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on November 9, 2017

This is my RigidbodyFPSController.cs :

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class RigidbodyFPSController : MonoBehaviour
{
    private Rigidbody rb;
 
    public float movementSpeed = 3f;
    public float jumpForce = 5f;
 
    float movX, movZ;
 
    bool canJump = false;
 
    void Start ()
    {
        rb = GetComponent<Rigidbody>();
    }
 
    void Update ()
    {
        movX = Input.GetAxisRaw("Horizontal") * movementSpeed;
        movZ = Input.GetAxisRaw("Vertical") * movementSpeed;
 
        transform.Translate(movX * Time.deltaTime, 0f, movZ * Time.deltaTime);
        if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
             transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime * 1.5f);
 
        if (Input.GetKeyDown(KeyCode.Space) && canJump)
        {
             rb.velocity = transform.up * jumpForce;
             canJump = false;
        }
    }
 
    void OnCollisionEnter (Collision collision)
    {
        canJump = true;
    }
 
    void OnCollisionExit (Collision collision)
    {
        canJump = false;
    }
 
}

How can I set the jump bool true until colliding with a plane and not a wall?

  • Liked by
Reply