2020-11-30

FixedUpdate doesn't register all keys (unity)

I ordered a book and I've been following it's instructions fully. The only problem is when I put my movements in FixedUpdate it doesn't register every key. I've read many answers to this but they all say put your inputs in Update and put all of the physics in FixedUpdate. I did just that but when I press the spacebar 30 times it only jumps around 5. This is some of my code (I apologize if it's readability is bad, I'm just starting out.):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBehavior : MonoBehaviour
{

    public GameObject bullet;

    public float moveSpeed = 10f;
    public float rotateSpeed = 10f;
    public float jumpVelocity = 5f;
    public float distanceToGround = 0.1f;
    public float bulletSpeed = 100f;

    public LayerMask groundLayer;

    private float vInput;
    private float hInput;

    private Rigidbody _rb;
    private CapsuleCollider _col;

    private bool isShooting;
    private bool isJumping;

    void Start()
    {
        _rb = GetComponent<Rigidbody>();
        _col = GetComponent<CapsuleCollider>();

        isShooting = false;
        isJumping = false;
    }


    void Update()
    {
        //player movement
        vInput = Input.GetAxis("Vertical") * moveSpeed;
        hInput = Input.GetAxis("Horizontal") * rotateSpeed;

        isJumping = Input.GetKeyDown(KeyCode.Space);

        //allows me to see if the spacer bar is registering
        if (isJumping)
        {
            Debug.Log("Jump");

        }

        isShooting = Input.GetMouseButtonDown(1);

        if (isShooting)
        {
            Debug.Log("Shoot");
        }
    }

    void FixedUpdate()
    {

        Vector3 rotation = Vector3.up * hInput;
        Quaternion angleRot = Quaternion.Euler(rotation * Time.fixedDeltaTime);
        _rb.MovePosition(this.transform.position + this.transform.forward * vInput * 
        Time.fixedDeltaTime);
        _rb.MoveRotation(_rb.rotation * angleRot);

        //determines if the player is on the ground and hit the spacebar. If so the player jumps.
        if (IsGrounded() && isJumping)
        {
            _rb.AddForce(Vector3.up * jumpVelocity, ForceMode.Impulse);

        //this allows isJumping to revert back to false 100% of the time

            isJumping = false;

            Debug.Log("The jump mechanic is working");
        }

        if (isShooting)
        {
            GameObject newBullet = Instantiate(bullet, this.transform.position + new Vector3(1, 0, 0), 
            this.transform.rotation) as GameObject;
            Rigidbody bulletRB = newBullet.GetComponent<Rigidbody>();
            bulletRB.velocity = this.transform.forward * bulletSpeed;

            isShooting = false;
            Debug.Log("The shooting mechanic is working");
        }
    }


    //determines if the player is on the ground
    bool IsGrounded()
    {
        Vector3 capsuleBottom = new Vector3(_col.bounds.center.x, _col.bounds.min.y, 
        _col.bounds.center.z);
        bool grounded = Physics.CheckCapsule(_col.bounds.center, capsuleBottom, distanceToGround, 
        groundLayer, QueryTriggerInteraction.Ignore);

        return grounded;
    }
}


from Recent Questions - Stack Overflow https://ift.tt/36iAH5v
https://ift.tt/eA8V8J

No comments:

Post a Comment