Hi guys,
Is it possible to hold a key down (do something) then let go and do something immediately after?
I have this script below. I would like to hold the “F” key down then when the user/player lets go it would reset back. At the moment I’m having to press the “F” key twice to get the effect. :?
using UnityEngine;
using System.Collections;
public class LookAtTarget : MonoBehaviour {
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.F)) {
transform.Rotate(Vector3.right, 180f, Space.Self);
}
if(Input.GetKeyUp (KeyCode.F)){
transform.Rotate(0, 0, 0, Space.Self);
}
}
}
GetKeyDown only is true only ONCE when the user presses the F key and only on the frame the key was pressed.
You want GetKey which remains true while a key is being pressed. Try this in your update instead.
//this will happen while the F key is pressed each frame
if (Input.GetKey (KeyCode.F))
{
transform.Rotate(Vector3.right, 180f, Space.Self);
}
//this will happen ONCE only when the F key is released
if(Input.GetKeyUp (KeyCode.F))
{
transform.Rotate(0, 0, 0, Space.Self);
}
Harv’s prediction : I foresee your next question has something to do with time.
Hi BPPHarv,
Thankx for the reply, sadly though that just flickers. Might be the transform.Rotate or like what you predict maybe a WaitForSeconds might do the trick?
Ah I see my prediction has come true.
Rotating the object like this in the update flips the object over… once each frame… so if you’re getting 60 frames/sec then this flips 60 times in a second.
Check the docs for Time.deltaTime examples if you want to rotate through 180degrees over time or yield WaitForSeconds to periodically flip it a full 180 degrees.
Of course its going to flicker… You are telling it to rotate 180 every frame… You need to tell us exactly what you are trying to do…
Just to look back while the “F” key is pressed then return back when unpressed.
In this case you have applied a rotation in the onkeydown section but have not undone it in the onkeyup section
if (Input.GetKeyDown (KeyCode.F))
{
transform.Rotate(Vector3.right, 180f, Space.Self);
}
if(Input.GetKeyUp (KeyCode.F))
{
transform.Rotate(Vector3.right, -180f, Space.Self);
}
EDIT: -180 looks funny cause it is… -180=180 but I left the sign in to make the point that your undoing your rotation.
BPPHarv, the code works except for the problem with the keys. I still have to press the “F” key twice, once to look back and again to look forward.
I put this script on a camera pressed F key… It looked behind while holding key. When I let go of key it rotated back. Seems to work for me. I did mine up in a blank test scene on the default MainCamera object they provide so you might have some kind of other interaction from another scripts in your scene. Other than that I dunno. Platform issue??
using UnityEngine;
using System.Collections;
public class LookAtTarget : MonoBehaviour
{
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.F))
{
transform.Rotate(Vector3.right, 180f, Space.Self);
}
if(Input.GetKeyUp (KeyCode.F))
{
transform.Rotate(Vector3.right, -180f, Space.Self);
}
}
}
I’m using the Standard Third Person asset that comes with Unity. Maybe if I disable the mouse look script that might work. Other than that, like you said, must be another script interfering.
Thankx for your help though, much appreciated.