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 nuri_amari · Jul 12, 2014 at 05:36 PM · inputprogrammingprefabskeyboard

Simultaneous Keyboard Input From Different Keys

Hello all,

I have some experience with game programming. However, I am new to Unity and C#. I am currently working on a space invaders like game in order to get the basics down. I have created a spaceship that can move left, right and shoot. One of my issues is the space ship can't move and shoot at the same time. This can get quite annoying especially since the space ship can shoot quite frequently. Any help would greatly appreciated. Here is the script that controls the space ship:

 using UnityEngine;
 using System.Collections;
 
 public class SpaceShipController : MonoBehaviour {
 
     public GameObject bulletObject;
 
     public GameManager gameManager;
 
     public float shipSpeed;
     public float bulletSpeed = 1;
 
     private Vector3 spaceShip;
 
     private Quaternion spaceShipRotation;
 
     private Vector3 bulletPosition;
 
     private int coolDown = 10;
 
     private bool moveRight = false;
     private bool moveLeft = false;
     private bool fire = false;
 
     // Use this for initialization
     void Start () {
     
         spaceShip = transform.position;
 
         spaceShipRotation = transform.rotation;
 
         bulletObject.transform.position = bulletPosition;
     }
     
     // Update is called once per frame
     void Update () {
 
         coolDown--;
 
         inputHandler();
 
         this.transform.position = spaceShip;
     }
 
     void inputHandler() {
 
         if (Input.GetKeyDown(KeyCode.RightArrow)) {
             
             moveRight = true;
             
         }
         
         if (Input.GetKeyDown(KeyCode.LeftArrow)) {
             
             moveLeft = true;
             
         }
         
         if (Input.GetKeyUp(KeyCode.RightArrow)) {
             
             moveRight = false;
             
         }
         
         if (Input.GetKeyUp(KeyCode.LeftArrow)) {
             
             moveLeft = false;
             
         }
         
         if (Input.GetKeyDown(KeyCode.Space)) {
             
             fire = true;
         }
         
         if (Input.GetKeyUp(KeyCode.Space)) {
             
             fire = false;
         }
         
         if (moveRight == true) {
             
             spaceShip.x += shipSpeed;
             
         }
         
         if (moveLeft == true) {
             
             spaceShip.x -= shipSpeed;
             
         }
         
 
         if (coolDown <= 0) {
 
             if (fire == true) {
                 
                 Fire ();
 
                 coolDown = 10;
             }
 
         }
     }
 
     void Fire () {
 
         for (var i = 0; i < 2; i++) {
 
             if (i == 0) {
 
                 spaceShip = new Vector3 (transform.position.x + 0.9f, transform.position.y + 0.9f, transform.position.z);
             }
 
             else if (i == 1) {
 
                 spaceShip = new Vector3 (transform.position.x - 0.9f, transform.position.y + 0.9f, transform.position.z);
             }
 
             Instantiate(bulletObject, spaceShip, spaceShipRotation);
 
             bulletObject.AddComponent<BulletBehaviour>();
 
             //gameManager.bullets[] = bulletObject;
 
             spaceShip = this.transform.position;
         }
     }
 }
 

The only other code I have written is a script that make the bullets fly forward. Here it is:

 using UnityEngine;
 using System.Collections;
 
 public class BulletBehaviour : MonoBehaviour {
 
     public SpaceShipController ship;
 
     private Vector3 shipPosition;
 
     // Use this for initialization
     void Start () {
     
         shipPosition = transform.position;
     }
     
     // Update is called once per frame
     void Update () {
     
         shipPosition.y += 1;
 
         transform.position = shipPosition;
     }
 }


Besides this I only have one other problem. My SpaceShipController takes a gameObject as a variable so that I knows what to spawn when you shoot. However, as far as I can tell this approach requires me to have the gameObject in the hierarchy. Is there a way to point the space ship class to the bullet object without having it already in the game by default? Once again any help would be greatly appreciated.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tswalk · Jul 12, 2014 at 07:28 PM

try just check for the combination like:

 if (Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.LeftArrow))
 {
   //move
   //shoot
 }


and put your logic in the condition...

Comment
Add comment · Share
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How do I update the position of different prefabs to the current position? 0 Answers

No keyboard events detected after any key is held. 0 Answers

Need help with disabiling keyboard inputs. 1 Answer

LeftControl key at runtime 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