I have a gameobject that has a constant forward force and I want to allow the player to slow the forward motion but I only want to allow that to happen 3 times then I want the player to have to wait for X amount of seconds before they are able to use the “stall()” function I created.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce = 2000f;
public float horizontalForce = 500f;
public float backForce = 1000f;
private bool canStall = true;
// Update is called once per frame
private void stall()
{
rb.AddForce(0, 0, -backForce * Time.deltaTime, ForceMode.Impulse);
}
void FixedUpdate ()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if(Input.GetKey("d"))
{
rb.AddForce(horizontalForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-horizontalForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKeyDown("s"))
stall();
if (rb.position.y < -2f)
{
FindObjectOfType<GameManager>().EngGame();
}
}
}