Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to stop person from flipping the car too often?

Discussion in 'Scripting' started by yotoprules, Jun 17, 2015.

  1. yotoprules

    yotoprules

    Joined:
    Jun 17, 2015
    Posts:
    10
    Hi I have this script where you press f and it flips the car upright

    But it is possible people could use it to cheat in the game.

    How can you make it so it only lets you do it 10 seconds after pressing it?
    Thanks.

    Code (JavaScript):
    1. function Update() {
    2. var Car : GameObject;
    3. Car = GameObject.Find("Car");
    4. if(Input.GetKeyDown(KeyCode.F)){
    5.     {
    6.         Car.transform.rotation.x = 0;
    7.         Car.transform.rotation.z = 0;
    8.     }
    9. }
    10. }
     
  2. SquarePieStudios

    SquarePieStudios

    Joined:
    Apr 22, 2015
    Posts:
    33
    Add this to your class:
    Code (CSharp):
    1.  
    2. //Set this in the inspector
    3. public float FlipCooldown = 10;
    4. //Use this to track the remaining cooldown
    5. private float _curFlipCooldown = 0;
    6.  
    7. void Update() {
    8. _curFlipCooldown = Mathf.Max(0, _curFlipCooldown - Time.deltaTime);
    9. if(_curFlipCooldown <= 0 && Input.GetKeyDown(KeyCode.F)) {
    10.     _curFlipCooldown = FlipCooldown;
    11.     //Do the stuff to the car here
    12. }
    In other words, what this does is set's the cooldown when the button is pressed and disallows pressing running the FlipLogic again until the cooldown has been completed.

    This is written in C#, but should be easy to convert to JS
     
  3. Deleted User

    Deleted User

    Guest

    "How to stop person from pressing a key too often?"

    Break their fingers.
     
  4. yotoprules

    yotoprules

    Joined:
    Jun 17, 2015
    Posts:
    10
    Thanks for the answer,
    how do i add it to the class and how do i convert it to javascript?
     
  5. yotoprules

    yotoprules

    Joined:
    Jun 17, 2015
    Posts:
    10
    fixed title.
     
  6. Deleted User

    Deleted User

    Guest

    "How to stop person from flipping the car too often?"

    My advice is not too stop them! If they can flip a car frequently they can probably kill you effortlessly.
     
  7. yotoprules

    yotoprules

    Joined:
    Jun 17, 2015
    Posts:
    10
    i don't have people in my game. if you were in the game there would be over 9000 cars running you over while flipping at the same time.
     
  8. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    First, I have to say that you don't want to run this inside the Update Loop.
    Code (csharp):
    1. Car = GameObject.Find("Car");
    What it does is this: AT EVERY FRAME, you are searching through ALL the gameObject on the scene for the one that is called Car. It is really inefficient and hurts performance.

    What you should do is cache the Game Object at the start of the game. (In Awake or in Start).

    Example (In C#, sorry... and untested, but should work!)
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ResetCarScript : MonoBehaviour
    5. {
    6.     GameObject car;
    7.     bool canReset = true;
    8.     float cooldownTimer = 10.0f;
    9.  
    10.     void Start()
    11.     {
    12.         car = GameObject.Find("car");
    13.         //I would recommend using GameObject.FindWithTag instead
    14.     }
    15.    
    16.     void Update()
    17.     {
    18.         if(Input.GetKeyDown(KeyCode.F))
    19.         {
    20.             if(canReset)
    21.                 ResetCar();
    22.         }  
    23.     }
    24.    
    25.     void ResetCar()
    26.     {
    27.         canReset = false;
    28.         Debug.Log ("RESET CAR!");
    29.         Invoke("CoolDown", cooldownTimer);
    30.     }
    31.    
    32.     void CoolDown()
    33.     {
    34.         canReset = true;
    35.     }
    36. }
    EDIT: Added class wrapper and changed quotation mark to corrects one.


    I tend to avoid using GameObject.Find all together. There usually always a better way of going.
    In your case, using a Tag and GameObject.FindWithtag would do the trick nicely. It's a lot more efficient.
    I didn't want to overload you too much, but I suggest you search on how to use Tags.
    Code (csharp):
    1. GameObject.FindWithTag
     
    Last edited: Jun 17, 2015
    yotoprules likes this.
  9. yotoprules

    yotoprules

    Joined:
    Jun 17, 2015
    Posts:
    10
    i tried that code but it just shows some errors.
     
  10. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    @yotoprules,

    I just tried the code, at it gave me two errors because the " " were instead this “ ”. I will replace the code to reflect the correct " ". Other than that the code works flawlessly.

    I also added the class wrapper..

    Just create a new C# script and name it ResetCarScript, copy-past my code and it should work.
     
  11. yotoprules

    yotoprules

    Joined:
    Jun 17, 2015
    Posts:
    10
    Assets/Scripts/ResetCarScript.cs(6,20): warning CS0414: The private field `ResetCarScript.car' is assigned but its value is never used
    When i press f it appears in the console window but the car doesn't actually flip.
    by the way thanks for the help so far!
     
  12. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Not sure I get why you have this error.. On what GameObject did you put the script?

    It doesn't reset, because I haven't applied your reset Logic

    You need to add your reset car logic in the ResetCar method, like so:

    Code (csharp):
    1. void ResetCar()
    2. {
    3.     canReset = false;
    4.     Debug.Log ("RESET CAR!");
    5.     car.rotation = Quaternion.identity;
    6.     Invoke("CoolDown", cooldownTimer);
    7. }
     
    yotoprules likes this.
  13. yotoprules

    yotoprules

    Joined:
    Jun 17, 2015
    Posts:
    10
    i put the script on an empty game object, do i need to place it on the car?
     
  14. yotoprules

    yotoprules

    Joined:
    Jun 17, 2015
    Posts:
    10
    now i have this error

    Assets/Scripts/ResetCarScript.cs(29,21): error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `rotation' and no extension method `rotation' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
     
  15. yotoprules

    yotoprules

    Joined:
    Jun 17, 2015
    Posts:
    10
    I FIXED IT! i used this code and put it where you put car.rotation.

    code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ResetCarScript : MonoBehaviour
    5. {
    6.     GameObject car;
    7.     bool canReset = true;
    8.     float cooldownTimer = 10.0f;
    9.  
    10.     void Start()
    11.     {
    12.         car = GameObject.Find("car");
    13.         //I would recommend using GameObject.FindWithTag instead
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if(Input.GetKeyDown(KeyCode.F))
    19.         {
    20.             if(canReset)
    21.                 ResetCar();
    22.         }
    23.     }
    24.  
    25.     void ResetCar()
    26.     {
    27.         canReset = false;
    28.         Debug.Log ("RESET CAR!");
    29.         Vector3 temp = transform.rotation.eulerAngles;
    30.         temp.x = 0.0f;
    31.         temp.z = 0.0f;
    32.         transform.rotation = Quaternion.Euler(temp);
    33.         Invoke("CoolDown", cooldownTimer);
    34.     }
    35.  
    36.     void CoolDown()
    37.     {
    38.         canReset = true;
    39.     }
    40. }
    got the rotation code from http://answers.unity3d.com/questions/604651/setting-value-of-transformrotation-in-code-c.html

    thanks to kellythomas and sluice!

    THANKS YOU SO MUCH!