So I’m working on a little sandbox project game (Following a tutorial/class online, but I’d like to change things up and add my own flavor to it).
This is my first time trying to make a game as well as my first time coding. Complete beginner , any advice/tips would be highly appreciated
The game is like a 3d platformer, I’d like to limit the amount of jumps the player is able to perform while in mid air. (which is most of the game)
I’d like the player to be able to jump maybe twice or three times. (would prefer to serialize a value so I could maybe change it in game for both the time and amount of jumps) I’m just stuck on how to actually implement the methods.
How can I implement a 2 second cool down delay on jumping? Currently the players able to jump infinitely.
Here’s the code i have so far is C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
[SerializeField] float mainThrust = 100f;
[SerializeField] float rotationThrust = 1f;
[SerializeField] AudioClip jump;
Rigidbody rb;
AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
ProcessThrust();
ProcessRotation();
}
void ProcessThrust()
{
if (Input.GetKey(KeyCode.Space))
{
rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
if(!audioSource.isPlaying)
{
audioSource.PlayOneShot(jump);
}
}
else
{
audioSource.Stop();
}
}
void ProcessRotation()
{
if (Input.GetKey(KeyCode.A))
{
ApplyRotation(rotationThrust);
}
else if (Input.GetKey(KeyCode.D))
{
ApplyRotation(-rotationThrust);
}
}
void ApplyRotation(float rotationThisFrame)
{
rb.freezeRotation = true; //freezing roation so we can manually rotate
transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
rb.freezeRotation = false; //unfreezing
}
}
Thanks for your time and help.