- Home /
How to push down a button and hold it through script?
I have two buttons, Tab and Shift. I'm using the Tab key as an accelerator.
I want to automate the "keep pressing it" part and make a shortcut to "Lock" it to being pressed down. For that, I tried using Shift.
So when I press shift ONCE, it toggles Tab being pressed down.
But I can't find a way to togle Tab though scripting.
A very important note is that I can't simply make Input.GetAxis("tab") = 1f or just copy the code I'm using on Tab to Shift, because I need to use the Gravity and Sensitivity of the button I set in the InputManager.
Here's the script
using UnityEngine;
using System.Collections;
public class ShipThrotle : MonoBehaviour
{
public float speed = 100f;
public float maxSpeed = 500f;
public float acceleration = 0.01f;
public float turnRate = 6005f;
public float angularDrag = 4f;
public bool isMoving = false;
Rigidbody myRB;
// Update is called once per frame
void Update ()
{
myRB = GetComponent<Rigidbody> ();
myRB.mass = acceleration;
myRB.angularDrag = angularDrag;
Vector3 vectorForce = transform.forward * speed * Time.deltaTime;
if(Input.GetAxis ("Throtle") > 0f)
{
GetComponent<Rigidbody>().drag = 0f;
GetComponent<Rigidbody>().AddForce (vectorForce);
myRB.velocity = Vector3.ClampMagnitude (myRB.velocity, maxSpeed);
}
if(Input.GetAxis ("Throtle") < 0f)
{
if(myRB.velocity.magnitude > Vector3.zero.magnitude)
{
GetComponent<Rigidbody>().drag = (GetComponent<Rigidbody>().drag + 0.01f) * 1.01f;
}
}
//Debug.Log (myRB.velocity);
//
if(Input.GetButton("RotateLeft"))
{
myRB.AddTorque(transform.forward * turnRate * 2 * Time.deltaTime);
}
if(Input.GetButton("RotateRight"))
{
myRB.AddTorque(-transform.forward * turnRate * 2 * Time.deltaTime);
}
//Debug.Log (Input.GetButton("RotateLeft"));
Debug.Log (myRB.velocity.magnitude + "Meters/Sec");
//Debug.Log (((myRB.velocity.z / 1000) * 60 * 60).ToString("F2") + "Km/Hour");
//Turn
float h = Input.GetAxis ("Horizontal") * turnRate * Time.deltaTime;
float v = Input.GetAxis ("Vertical") * turnRate * Time.deltaTime;
myRB.AddTorque (transform.up * h);
myRB.AddTorque (-transform.right * v);
}
}
Could anybody help me, please?
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Increasing value by one every second, if right arrow is pressed - C# 3 Answers
Key for GUI.Button 2 Answers
Multiple Cars not working 1 Answer