Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jerobinson · May 26, 2013 at 01:28 PM · build

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jerobinson · May 27, 2013 at 09:22 AM 0
Share

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...

avatar image Kajos · May 27, 2013 at 01:35 PM 0
Share

Is it possible you were calling $$anonymous$$ove in FixedUpdate? Some engines have problems with that.

avatar image Prodigga · May 27, 2013 at 01:53 PM 0
Share

^ thats the only thing I can think of. FixedUpdate runs every physics step, and your keyboard input would be falling inbetween frames.

avatar image jerobinson · May 28, 2013 at 01:13 AM 0
Share

No, I was using Update(). Not sure what happened, but it works now :)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges