Question by
blue_tronic · Aug 11, 2017 at 07:20 AM ·
c#controllercontrols
Freeze A Value
I am working in void Update, but there is a value that I only want to update when there is no arrow key input being pressed. If an arrow key is being pressed, I want the value to freeze until it is released, as if the string was updating only when no input is detected rather than every frame.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
private Rigidbody2D rb;
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
}
public float thrust;
void Update ()
{
float horizontal = Input.GetAxis ("Horizontal");
float horizontal2 = Input.GetAxis ("Horizontal2");
float vertical = Input.GetAxis ("Vertical");
if (transform.rotation.eulerAngles.z >= 90 && transform.rotation.eulerAngles.z <= 270) { //facing left
horizontal = horizontal * -1; //invert horizontal arrow keys
horizontal2 = horizontal2 * -1; //invert mouse buttons
}
if (transform.rotation.eulerAngles.z >= 180 && transform.rotation.eulerAngles.z <= 360) { //facing down
vertical = vertical * -1; //invert vertical arrow keys
}
thrust = horizontal + horizontal2 + vertical; //combine arrow key and mouse inputs
thrust = Mathf.Clamp (thrust, -1, 1);
rb.AddForce (transform.right * thrust * speed); //apply thrust
}
}
Here is the code in question. I want to freeze the values "horizontal" "horizontal2" and "vertical" (lines 23, 24 and 27) so that they do not invert their values while rb.AddForce (line 32) is being calculated
Comment
So, what's the question? Why can't you just check if Input.GetButton(x) is false?
Please, post the actual code, not a screenshot of it if you want others to test what you have done and help you.