Question by
KAYBRAC · Apr 06, 2017 at 04:22 PM ·
scripting problem
Hi here is my scrip for ingame steeringwheel. Why th wheel dont spin back to startposition ?
var velocidade = 30; static var origRotation : Quaternion;
function Start () { origRotation = transform.localRotation; }
function Update () {
if (Input.GetKey(KeyCode.A)){
transform.Rotate(Vector3.forward * velocidade * Time.deltaTime);
transform.localRotation = Quaternion.Slerp(transform.localRotation, origRotation, Time.deltaTime);
}
if (Input.GetKey(KeyCode.D)){
transform.Rotate(-Vector3.forward * velocidade * Time.deltaTime);
transform.localRotation = Quaternion.Slerp(transform.localRotation, origRotation, Time.deltaTime);
}
}
Comment
Answer by KKS21199 · Apr 06, 2017 at 05:57 PM
You are only rotating when the key is pressed.
var velocidade = 30;
static var origRotation : Quaternion;
function Start () { origRotation = transform.localRotation; }
function Update () {
if (Input.GetKey(KeyCode.A)){
transform.Rotate(Vector3.forward * velocidade * Time.deltaTime);
}
elseif (Input.GetKey(KeyCode.D)){
transform.Rotate(-Vector3.forward * velocidade * Time.deltaTime);
}
else
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, origRotation, Time.deltaTime);
}
If for some reason you want to see if both $$anonymous$$eyCode.A and $$anonymous$$eyCode.D are pressed at the same time, then declare a bool outside the function and set it to true within the if functions when the key is pressed.
public bool pressed = false;
function Update () {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)){
pressed = true;
transform.Rotate(Vector3.forward * velocidade * Time.deltaTime);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)){
pressed = true;
transform.Rotate(-Vector3.forward * velocidade * Time.deltaTime);
}
if(!pressed)
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, origRotation, Time.deltaTime);
}
pressed = false;
}