- Home /
unity how to use two keycodes in c# in one update i use unity 2017.4.2f2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EscToScane : MonoBehaviour {
public Texture2D cursorTexture;
public Texture2D cursorTexture2;
public CursorMode cursorMode = CursorMode.Auto;
public Vector2 hotSpot = Vector2.zero;
public GameObject Menu;
public GameObject SettingsLite;
public bool paused;
void Start () {
paused = false;
}
void Update () {
if (Input.GetKeyDown (KeyCode.Escape))
paused = !paused;
{
Menu.SetActive (true);
}
if (paused)
{
Cursor.SetCursor (cursorTexture, hotSpot, cursorMode);
Time.timeScale = 0;
}
else if (!paused){
Time.timeScale = 1;
Cursor.SetCursor (cursorTexture2, hotSpot, cursorMode);
Menu.SetActive (false);
SettingsLite.SetActive (false);
}
}
void FixedUpdate () {
if (Input.GetKeyDown (KeyCode.JoystickButton6))
paused = !paused;
{
Menu.SetActive (true);
}
if (paused)
{
Cursor.SetCursor (cursorTexture, hotSpot, cursorMode);
Time.timeScale = 0;
}
else if (!paused){
Time.timeScale = 1;
Cursor.SetCursor (cursorTexture2, hotSpot, cursorMode);
Menu.SetActive (false);
SettingsLite.SetActive (false);
}
}
public void Pause()
{
paused = !paused;
if (paused)
{
Time.timeScale = 0;
}
else if (!paused)
{
Time.timeScale = 1;
}
}
}
I'm not sure what you want to do, but the first if
in Update()
and FixedUpdate()
do not affect the blocks with $$anonymous$$enu.SetActive(true);
, because there is an instruction after them. The if
statement only affects the first instruction or block directly after them. To avoid this problem, ALWAYS put the instruction after if
in a block (between { }
).
If I understand your question correctly, you want the boolean OR operation ( ||
) in if
, something like this:
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Escape) || Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.JoystickButton6))
Further recommendations: - don't check input in FixedUpdate()
, because it runs relatively slow for user input. Do all the input handling in Update()
- code that gets repeated in different parts is a perfect candidate to be moved into a separate function (for example what you have in Update()
and FixedUpdate()
). That way if something changes, you only need to change it in one place, and it also makes the code easier to read
- (related to the above) do things where they belong: if you have a Pause()
function that toggles the value of paused
and then sets things based on the updated value, then don't set general things based on paused
in other places (e.g. Update()
). For example, the setting of the cursor texture, activating/deactivating the $$anonymous$$enu
, etc. can all go into Pause()
, and Update()
would only need to call Pause()
Can you develop what you exactly want to do I don't really understand where you want to go.
Answer by SohailBukhari · Jun 07, 2018 at 07:36 AM
Mmm,, you can use the OR condition in the if statement, which will return true if any of them true, so use if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.JoystickButton6))
in the Update.
OR Eample if (a == b || a == c) means "if a is equal to b OR a is equal to c"
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.JoystickButton6))
paused = !paused;
{
Menu.SetActive(true);
}
if (paused)
{
Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
Time.timeScale = 0;
}
else if (!paused)
{
Time.timeScale = 1;
Cursor.SetCursor(cursorTexture2, hotSpot, cursorMode);
Menu.SetActive(false);
SettingsLite.SetActive(false);
}
}
use the paused = !paused;
inside the brackets.
Write clear code you are repeating the same code at many places so instead of writing repeating code make functions and pass their parameters on change.
i use private modifier that the other instance will not access it. have a look at the Access modifiers http://csharp.net-informations.com/language/csharp-access-specifiers.htm