How to i use get key to use the turn?
I need to make it so it only turns when i press x plz help.
using UnityEngine;
using System.Collections;
public class Lerp180Left : MonoBehaviour
{
public Vector3 axis = new Vector3( 0, 1, 0 );
public float degrees = 90f;
public float timespan = 1f;
private float _rotated = 0;
private Vector3 _rotationVector;
public void Start()
{
axis.Normalize();
_rotationVector = axis * degrees;
}
public void Update()
{
_rotated += degrees * (Time.deltaTime / timespan);
if ( degrees > _rotated )
transform.Rotate( _rotationVector * (Time.deltaTime / timespan) );
}
}
Use Input.Get$$anonymous$$ey or GetButton something and than rotate , not sure if you have to put this line _rotated += degrees * (Time.deltaTime / timespan); as well in the if keypressed
i know i need to do that i tried but i don't understand were to put the code.
in the update function I imagine add a key command if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.N)) {
Answer by Jessespike · Aug 22, 2016 at 04:56 AM
void Update() {
if (Input.GetKey(KeyCode.X))
{
_rotated += degrees * (Time.deltaTime / timespan);
if (degrees > _rotated)
transform.Rotate(_rotationVector * (Time.deltaTime / timespan));
}
}
sorry but this is not working either it make me hold down the key D:
If you want to rotate completely to an extent once a key is pressed I would recommend the use of coroutine. Call it once with coroutine and and use boolean as well to not to calling it again and again , if the button is kept on as pressed
bool keyPressed = false;
void Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X))
{
keyPressed = true;
}
if (keyPressed)
{
_rotated += degrees * (Time.deltaTime / timespan);
if (degrees > _rotated)
transform.Rotate(_rotationVector * (Time.deltaTime / timespan));
}
}
this works! but it only works once! i need it to be repeatable please.