Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I watch 'Imphenzia's' videos I see my and his code same but his only jump once, but mine jumps multiple times and breaks the game. his video's link is here
[DELETED]

What I have tried:

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using UnityEngine;

public class Player : MonoBehaviour
{
    private bool JumpKeyWasPressed;
    private float HorizontalInput;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            JumpKeyWasPressed = true;
        }
        HorizontalInput = Input.GetAxis("Horizontal");

    }
    void FixedUpdate()
    {
        if (JumpKeyWasPressed)
        {
            GetComponent<Rigidbody>().AddForce(Vector3.up * 5, ForceMode.VelocityChange);
            JumpKeyWasPressed = false;
        }
    }
}
Posted
Updated 19-Oct-20 23:39pm
v2
Comments
F-ES Sitecore 20-Oct-20 14:40pm    
GetKeyDown will only return true once per frame so I don't see how it will do it multiple times unless you have some crazy fps issues. Try changing the line to

if (!JumpKeyWasPressed && Input.GetKeyDown(KeyCode.Space))

and see if that makes a difference.

1 solution

If you got the code from a Youtube video, then use the forum at the bottom of the video to talk to the author: he should know his code a lot better than a random website will.

If he doesn't know, or he doesn't reply then you will know his code is rubbish and you should look again.

In general, Youtube development tutorials are a waste of time and energy: they are produced by people who know nothing about producing a video, and often less than that about the subject matter they are presenting. Most of 'em are only there to get likes and subscribes - monetization is the name of the game!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900