- Home /
Game test-build sometimes not recognising key inputs?
Hey guys, I'm working on a simple third person game. In the game you can talk to a NPC. This is how it works:
The players script does a Physics.OverlapSphere() to check if it's near the NPC.
If he is near, then it waits for Input.GetKeyDown(keycode.Return) to initiate a very simple dialogue system.
This brings up a simple GUI which displays the message.
When I play the game in the editor window, everything works perfectly. However, after test-building the game, this only works about 50% of the time. It appears that the key press does not register.
My player only has one script attached, so it is quite long. Would rewriting it into separate classes make it more efficient? I have been using Unity for over a year now. I am alright at scripting, but I learned through trial and error so I may be missing something important.
Here is a copy of my players script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class Player : MonoBehaviour {
//Player control keycodes
public KeyCode forward = KeyCode.W;
public KeyCode left = KeyCode.A;
public KeyCode right = KeyCode.D;
public KeyCode jump = KeyCode.Space;
//Action keycodes
public KeyCode action = KeyCode.Return;
//GUI
public GUISkin display;
//CharacterController component
private CharacterController controller;
//Player movement variables
private bool canMove = true;
private Vector3 moveDir = Vector3.zero;
private float moveSpeed = 6.0f;
private float turnSpeed = 3.0f;
private float jumpForce = 8.0f;
private float gravForce = -20.0f;
//Player statistics
private int maxHealth = 3;
private int curHealth;
private int gems;
private float showDelay = 2.5f;
private float showStats = 0.0f;
//Actions
private float sensorRange = 2.0f;
private string pickupTag = "Pickup";
private GameObject pickup;
private string npcTag = "NPC";
private GameObject npc;
private bool nearNpc = false;
private bool isTalking = false;
private string curMessage;
private int messageNumber = 0;
//Screen resolution and GUI
private float originalWidth = 1024.0f;
private float originalHeight = 768.0f;
private float displayX;
private float displayY;
private float speechBoxX;
private float speechBoxY;
void Start() {
controller = gameObject.GetComponent<CharacterController>();
SetGUI(originalWidth, originalHeight);
}
void Update() {
if(controller.isGrounded) {
if(canMove) {
Move();
}
if(nearNpc) {
if(Input.GetKeyDown(action)) {
Talk();
}
}
}
senseAction();
moveDir.y += gravForce * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
void Move() {
if(Input.GetKey(forward)) {
moveDir = new Vector3(0.0f, 0.0f, moveSpeed);
moveDir = transform.TransformDirection(moveDir);
}
else {
moveDir = new Vector3(0.0f, 0.0f, 0.0f);
moveDir = transform.TransformDirection(moveDir);
}
if(Input.GetKey(right)) {
transform.Rotate(0.0f, turnSpeed * 90.0f * Time.deltaTime, 0.0f);
}
if(Input.GetKey(left)) {
transform.Rotate(0.0f, -turnSpeed * 90.0f * Time.deltaTime, 0.0f);
}
if(Input.GetKeyDown(jump)) {
moveDir.y = jumpForce;
}
}
void senseAction() {
Vector3 origin = transform.position + controller.center;
Collider[] hitColliders = Physics.OverlapSphere(origin, sensorRange);
for(int i = 0; i < hitColliders.Length; i++) {
if(hitColliders[i].tag == pickupTag) {
pickup = hitColliders[i].gameObject;
pickup.SendMessage("AttractGem", true, SendMessageOptions.RequireReceiver);
}
if(hitColliders[i].tag == npcTag) {
npc = hitColliders[i].gameObject;
nearNpc = true;
}
else {
nearNpc = false;
}
}
}
void Talk() {
NPC dialogue = npc.GetComponent<NPC>();
if(messageNumber < dialogue.speech.Length) {
isTalking = true;
canMove = false;
moveDir = new Vector3(0.0f, 0.0f, 0.0f);
moveDir = transform.TransformDirection(moveDir);
showStats = showDelay;
curMessage = dialogue.speech[messageNumber];
messageNumber ++;
}
else {
isTalking = false;
canMove = true;
messageNumber = 0;
}
}
public void AdjHealth(int adj) {
curHealth += adj;
if(curHealth <= 0) {
curHealth = 0;
StartCoroutine(Die());
}
if(curHealth > maxHealth) {
curHealth = maxHealth;
}
showStats = showDelay;
}
public void AdjGems(int adj) {
gems += adj;
if(gems < 0) {
gems = 0;
}
showStats = showDelay;
}
IEnumerator Die() {
canMove = false;
yield return new WaitForSeconds(1.0f);
Debug.Log("You Are Dead");
}
void SetGUI(float width, float height) {
displayX = width * 0.20f;
displayY = height * 0.05f;
speechBoxX = width * 0.5f;
speechBoxY = height * 0.15f;
}
void OnGUI() {
Vector2 ratio = new Vector2(Screen.width/originalWidth , Screen.height/originalHeight);
Matrix4x4 guiMatrix = Matrix4x4.identity;
guiMatrix.SetTRS(new Vector3(1.0f, 1.0f, 1.0f), Quaternion.identity, new Vector3(ratio.x, ratio.y, 1.0f));
GUI.matrix = guiMatrix;
GUI.skin = display;
if(showStats > 0.0f) {
GUI.Box(new Rect(25.0f, 25.0f, displayX, displayY), "Health: " + curHealth);
GUI.Box(new Rect(originalWidth - displayX - 25.0f, 25.0f, displayX, displayY), "Gems: " + gems);
if(!isTalking) {
showStats -= Time.deltaTime;
}
}
if(isTalking) {
GUI.Box(new Rect(originalWidth * 0.5f - speechBoxX * 0.5f, originalHeight - speechBoxY - 25.0f, speechBoxX, speechBoxY), curMessage + "\n\nPress Action To Continue");
}
GUI.matrix = Matrix4x4.identity;
}
}
Thanks for your time.
Ok, well I removed all of the non-essentials of the script, ie reduced number of private variables, but kept the same functionality. It works now in both development build and editor. Not really sure why it didn't work before, but it was recommended to me that I should reload the scene before building the game again...
Is it possible you were calling $$anonymous$$ove in FixedUpdate? Some engines have problems with that.
^ thats the only thing I can think of. FixedUpdate runs every physics step, and your keyboard input would be falling inbetween frames.
No, I was using Update(). Not sure what happened, but it works now :)
Your answer
Follow this Question
Related Questions
Android Building problem 1 Answer
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
GUI.Button not showing up 1 Answer
Disable JIT compilation in the Editor (Enable Full-AOT mode) 0 Answers