Setting up a gear system of sorts...
I really hate being a burden, especially when I don't know what exactly to do in this case, but I'm not sure what to do here. In brief, what I'm trying to do is alter my control script (which is already a mess of code snippets taken from various online sources) to allow the player to select about 6 speeds (Reverse, Stop, 1/4, 1/2, 3/4, and Full.) while having to only press the button once. (Bridge Commander's control scheme is a perfect example of this.) So far I've managed to program the exact speeds that I want and I've successfully bound them to the buttons, but I have no idea how to maintain those speeds when I let go.
public float xFactor = 70f;
public float yFactor = 150f;
public float zFactor = 3f;
public float AmbientSpeed = 100.0f;
public float RotationSpeed = 200.0f;
void Update()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * xFactor;
var z = 0.5f;
var y = Input.GetAxis("Vertical") * Time.deltaTime * yFactor;
if (Input.GetKey(KeyCode.Keypad0))
z = Time.deltaTime * 0;
if (Input.GetKey(KeyCode.Keypad1))
z = Time.deltaTime * 50;
if (Input.GetKey(KeyCode.Keypad2))
z = Time.deltaTime * 100;
if (Input.GetKey(KeyCode.Keypad3))
z = Time.deltaTime * 150;
if (Input.GetKey(KeyCode.Keypad4))
z = Time.deltaTime * 200;
if (Input.GetKey(KeyCode.R))
z = Time.deltaTime * -50;
transform.Rotate(y, 0, 0);
transform.Translate(0, 0, z);
transform.Rotate(0, x, 0);
Quaternion AddRot = Quaternion.identity;
float roll = 0;
float pitch = 0;
float yaw = 0;
roll = Input.GetAxis("Roll") * (Time.deltaTime * RotationSpeed);
pitch = Input.GetAxis("Pitch") * (Time.deltaTime * RotationSpeed);
yaw = Input.GetAxis("Yaw") * (Time.deltaTime * RotationSpeed);
AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
GetComponent<Rigidbody>().rotation *= AddRot;
Vector3 AddPos = Vector3.forward;
}
Can anybody lend me a hand here?
Answer by b1gry4n · Oct 17, 2016 at 01:52 AM
you dont need to multiply your x or y values by time.deltatime. GetAxis is going to return a value between -1 and 1. -1 being left/down, 0 being no input, and 1 being right/up. Multiplying it by Time.deltaTime (which will be some decimal value like 0.3235235 or something) does nothing at this point other than make control harder. You only need to multiply the very last step by Time.deltaTime, being the actual function to rotate or move the object.
You need to store the selected speed. First off...use "GetKeyUp" or GetKeyDown"...The reason being "GetKey" requires constant pressing for it to be "true". up or down fires when the button is either up or down. The "Z" value, make it a variable.
public float speed;
Now when the button is pressed, change the speed variable to your desired speed.
if (Input.GetKeyDown(KeyCode.Keypad4))
speed = 200;
and finally, where you are actually applying the speed (take note, i didnt multiply by Time.deltaTime in the key press)
transform.Translate(0, 0, speed * Time.deltaTime);
Alright, that didn't quite help. It's still not moving at the set speeds whenever I let go. Did I implement your fixes incorrectly? Z is already a variable, and I got rid of the deltaTimes.
z is not already a stored variable. You are declaring it every frame at the top of update. The following frame after you let go of any button, z is declared new and reset to 0.5
public float xFactor = 70f;
public float yFactor = 150f;
public float zFactor = 3f;
public float AmbientSpeed = 100.0f;
public float RotationSpeed = 200.0f;
float speed = 0.0f;
void Update()
{
//var x = Input.GetAxis("Horizontal") * Time.deltaTime * xFactor;
//var z = 0.5f;
//var y = Input.GetAxis("Vertical") * Time.deltaTime * yFactor;
//if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad0))
// z = Time.deltaTime * 0;
//if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad1))
// z = Time.deltaTime * 50;
//if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad2))
// z = Time.deltaTime * 100;
//if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad3))
// z = Time.deltaTime * 150;
//if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad4))
// z = Time.deltaTime * 200;
//if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.R))
// z = Time.deltaTime * -50;
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad0))
{
speed = 0;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad1))
{
speed = 50;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad2))
{
speed = 100;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad3))
{
speed = 150;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad4))
{
speed = 200;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.R))
{
speed = -50;
}
//transform.Rotate(y, 0, 0);
//transform.Translate(0, 0, z);
//transform.Rotate(0, x, 0);
Vector3 movement = new Vector3(Input.GetAxis("Horizontal") * xFactor, Input.GetAxis("Vertical") * yFactor, speed * zFactor);
transform.Rotate(movement.y * Time.deltaTime, movement.x * Time.deltaTime, 0);
transform.Translate(0, 0, movement.z * Time.deltaTime);
Quaternion AddRot = Quaternion.identity;
float roll = 0;
float pitch = 0;
float yaw = 0;
roll = Input.GetAxis("Roll") * (Time.deltaTime * RotationSpeed);
pitch = Input.GetAxis("$$anonymous$$ch") * (Time.deltaTime * RotationSpeed);
yaw = Input.GetAxis("Yaw") * (Time.deltaTime * RotationSpeed);
AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
GetComponent<Rigidbody>().rotation *= AddRot;
Vector3 AddPos = Vector3.forward;
}