- Home /
Getting Input and do Action Until..
Hey all,
This little problem is driving me insane. I want the user to press the Alpha-Character "1", and then while the vehicles speed is less than 3, increase the speed without having to hold down the "1" key.
I just want the user to press the key, and not have to hold it, and I can't seem to find a good example somewhere of this type of control.
Here's my code in my Update() method..
using UnityEngine;
using System.Collections;
public class speedMod : MonoBehaviour {
private Vector3 velocity;
public Transform sub;
public float subSpeed;
public bool moveSpeed;
// Use this for initialization
void Start () {
moveSpeed = false;
}
// Update is called once per frame
void Update () {
Debug.Log (moveSpeed);
Debug.Log (subSpeed);
if(Input.GetKeyDown(KeyCode.Alpha1)) {
moveSpeed = true;
if(moveSpeed && subSpeed<=.1) {
subSpeed+=0.002f * Time.deltaTime;
} else if(subSpeed==.1){
moveSpeed = false;
}
}
transform.Translate(0, 0, subSpeed);
}
void OnGUI() {
GUI.Label(new Rect(10, 10, 150, 20), "Spd: "+subSpeed.ToString());
}
}
Edit: This is my current version of the script. Right now it's simply attached to a cube.
Answer by whydoidoit · Jul 28, 2012 at 04:26 PM
var increaseSpeed = false; // script level variable
...
if(Input.GetKeyDown(KeyCode.Alpha1)) increaseSpeed = true;
if(increaseSpeed && subSpeed < .1) {
//increase the speed
} else {
increaseSpeed = false;
}
Unfortunately this does not do the trick. It does for almost everything except for increasing the translation of the position.
You still have a transform.Translate() after it right? Hmmm should work - wonder what I missed...
using UnityEngine;
using System.Collections;
public class speed$$anonymous$$od : $$anonymous$$onoBehaviour {
private Vector3 velocity;
public Transform sub;
public float subSpeed;
public bool moveSpeed;
// Use this for initialization
void Start () {
moveSpeed = false;
}
// Update is called once per frame
void Update () {
Debug.Log (moveSpeed);
Debug.Log (subSpeed);
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha1)) {
moveSpeed = true;
}
if(moveSpeed && subSpeed<=.1) {
subSpeed+=0.002f * Time.deltaTime;
} else {
moveSpeed = false;
}
transform.Translate(0, 0, subSpeed);
}
void OnGUI() {
GUI.Label(new Rect(10, 10, 150, 20), "Spd: "+subSpeed.ToString());
}
}
Note how the if just controls setting moveSpeed - it does not then contain the rest of the code which is gated by that boolean set in the if. Speed will increase to 0.1 and will then stop increasing after the key is pressed. Currently there is no way to reverse this action.
Answer by Muuskii · Sep 05, 2012 at 11:13 PM
There seems to be a lot of unneeded hassle going on here. All you need is to set a target speed right?
using UnityEngine;
using System.Collections;
public class speedMod : MonoBehaviour
{
private Vector3 velocity;
public float targetSpeed;
public float maxSpeed = 5.0f;
public float acceleration = 1.0f;
void Update()
{
if(Input.GetKeyDown(KeyCode.BackQuote) ) targetSpeed = 0;
for( KeyCode current = KeyCode.Alpha0; current <= KeyCode.Alpha9; current++)
{
if(Input.GetKeyDown(current) )
{
if(current == KeyCode.Alpha0)targetSpeed = 10;
else targetSpeed = current - KeyCode.Alpha0;
break;
}
}
if( Input.GetKeyDown(KeyCode.Minus) )targetSpeed--;
if( Input.GetKeyDown(KeyCode.Plus) )targetSpeed++;
float realTargetSpeed = targetSpeed/10 * maxSpeed;
//Get which way to accelerate
float sign = Mathf.Sign(velocity.sqrMagnitude - realTargetSpeed*realTargetSpeed);
if(sign != 0)
{
float deltaVelocity = velocity.magnitude - realTargetSpeed;
if(deltaVelocity > acceleration)velocity -= velocity.normalized * acceleration * sign;
else velocity = velocity.normalized * realTargetSpeed;
}
//put your method of movement here
move(velocity);
}
}
This should make your velocity between 0 - maxspeed
Your answer
