• 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 /
  • Help Room /
avatar image
0
Question by abentley · Sep 28, 2017 at 04:20 AM · keyboard input

Can someone help me with my code?

I am new to coding and I am still trying to learn and I got stuck with a couple problems I'm having. I'm making a game surround a spaceship that I made in blender. This ship has eight thrusters on it which is where I am having my problem. I want my particle system to activate when I press down a certain key and then to stop when the key is lifted. I have gotten the thrusters to activate when the key is pressed and deactivate when the key is pressed again, but OnKeyUp is not working for me. I would also like an audio clip to play only while the key is pressed down as well, again it activates, but does not shut off. Below is a copy of the code I'm working with:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Audio;

public class PLAYERMOVEMENT : MonoBehaviour {

 public float PlayerUp = 5.0f;
 public float PlayerRight = 5.0f;
 public float PlayerForward = 5.0f;
 public bool ThrustersActive = false;
 public Rigidbody PlayerBody;
 public ParticleSystem Left1;
 public ParticleSystem Left2;
 public ParticleSystem Right1;
 public ParticleSystem Right2;
 public ParticleSystem Forward1;
 public ParticleSystem Forward2;
 public ParticleSystem Backward1;
 public ParticleSystem Backward2;

 void FixedUpdate() {


     //Player Horizontal Movement
     if (Input.GetKeyDown( KeyCode.W )) //Forward Movement
     {
         ThrusterToggle();
         PlayerBody.AddForce(0, 0, PlayerForward * Time.deltaTime);
         ThrusterDeactivation();
     }
     if (Input.GetKeyDown( KeyCode.S )) //Backwards Movement
     {
         ThrusterToggle();
         PlayerBody.AddForce(0, 0, -PlayerForward * Time.deltaTime);
     }
     if (Input.GetKeyDown( KeyCode.A )) //Left Movement
     {
         ThrusterToggle();
         PlayerBody.AddForce(-PlayerRight * Time.deltaTime, 0, 0);
     }
     if (Input.GetKeyDown( KeyCode.D )) //Right Movement
     {
         ThrusterToggle();
         PlayerBody.AddForce(PlayerRight * Time.deltaTime, 0, 0);
     }

     //Player Vertical Movement
     if (Input.GetKeyDown( KeyCode.Q )) //Upward Movement
     {
         ThrusterToggle();
         PlayerBody.AddForce(0, PlayerUp * Time.deltaTime, 0);
     }

 }

 public void ThrusterDeactivation () {

     //Horizontal Thruster Deactivation
     if (Input.GetKeyUp( KeyCode.W ))
     {
         ThrusterToggle();
     }
     if (Input.GetKeyUp( KeyCode.S ))
     {
         ThrusterToggle();
     }
     if (Input.GetKeyUp( KeyCode.A ))
     {
         ThrusterToggle();
     }
     if (Input.GetKeyUp( KeyCode.D ))
     {
         ThrusterToggle();
     }

     //Vertical Thruster Deactivation
     if (Input.GetKeyUp( KeyCode.Q ))
     {
         ThrusterToggle();
     }

 }

 //Thruster Activation Toggle
 public void ThrusterToggle() {

     //Forward Thrusters Toggle
     if (Input.GetKey( KeyCode.W ))
     {
         if (Forward1.isStopped)
         {
             Forward1.Play();
             Forward2.Play();
             ThrustersActive = true;
             AudioCaller();
         }
         else
         {
             Forward1.Stop();
             Forward2.Stop();
             ThrustersActive = false;
             AudioCaller();
         }
     }

     //Backward Thrusters Toggle
     if (Input.GetKey( KeyCode.S ))
     {
         if (Backward1.isStopped)
         {
             Backward1.Play();
             Backward2.Play();
         }
         else
         {
             Backward1.Stop();
             Backward2.Stop();
         }
     }

     //Right Thrusters Toggle
     if (Input.GetKey( KeyCode.A ))
     {
         if (Right1.isStopped)
         {
             Right1.Play();
             Right2.Play();
         }
         else
         {
             Right1.Stop();
             Right2.Stop();
         }
     }

     //Left Thrusters Toggle
     if (Input.GetKey( KeyCode.D ))
     {
         if (Left1.isStopped)
         {
             Left1.Play();
             Left2.Play();
         }
         else
         {
             Left1.Stop();
             Left2.Stop();
         }
     }

     //Upward Thrusters Toggle
     if (Input.GetKey( KeyCode.Q ))
     {
         if (Forward1.isStopped)
         {
             Forward1.Play();
             Forward2.Play();
             Backward1.Play();
             Backward2.Play();
             Left1.Play();
             Left2.Play();
             Right1.Play();
             Right2.Play();
         }
         else
         {
             Forward1.Stop();
             Forward2.Stop();
             Backward1.Stop();
             Backward2.Stop();
             Left1.Stop();
             Left2.Stop();
             Right1.Stop();
             Right2.Stop();
         }
     }
 }

 public void AudioCaller() {

     if ( ThrustersActive == true )
     {
         FindObjectOfType<AudioManager>().PlayAudio("ThrustersActivate");
     }
     else if ( ThrustersActive == false )
     {
         FindObjectOfType<AudioManager>().StopAudio();
     }
 }

}

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
Best Answer

Answer by Hellium · Sep 28, 2017 at 08:53 AM

Here is how I would have done it. I guess you may need some adjustments, but you will get the idea. I've reworked completely your script, so tell me if you don't understand something.

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Thruster
 {
     public Vector3 ThrustDirection;
     public KeyCode KeyCode;
     public ParticleSystem[] ParticleSystems;
     public string AudioClip; // Specify `ThrustersActivate`  if you want a sound to be played when thrusters are turned on and off.
 
     public void TurnOn()
     {
         for ( int i = 0 ; i < ParticleSystems.Length ; ++i )
             ParticleSystems[i].Play();
 
         if( !AudioClip.Equals( string.Empty) )
             GameObject.FindObjectOfType<AudioManager>().PlayAudio( AudioClip );
     }
 
     public void TurnOff()
     {
         for ( int i = 0 ; i < ParticleSystems.Length ; ++i )
             ParticleSystems[i].Stop();
 
         if ( !AudioClip.Equals( string.Empty ) )
             GameObject.FindObjectOfType<AudioManager>().StopAudio();
     }
 
 }
 
 public class PlayerMovement : MonoBehaviour
 {
     public Rigidbody PlayerBody;

     // In the inspector, you will be able to specify your thrusters more easily
     public Thruster[] thrusters;
 
     void FixedUpdate()
     {
         for ( int i = 0 ; i < thrusters.Length ; ++i )
         {
             if ( Input.GetKeyUp( thrusters[i].KeyCode ) )
             {
                 thrusters[i].TurnOff();
             }
             else
             {
                 if ( Input.GetKeyDown( thrusters[i].KeyCode ) )
                 {
                     thrusters[i].TurnOn();
                 }
                 if ( Input.GetKey( thrusters[i].KeyCode ) )
                 {
                     PlayerBody.AddForce( thrusters[i].ThrustDirection * Time.deltaTime );
                 }
             }
         }
     }
 }
Comment
Add comment · Show 1 · 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
avatar image abentley · Sep 29, 2017 at 04:55 AM 0
Share

Thank you!! This is perfect! It’s a much cleaner solution to my problem!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

114 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change the fullscreen keyboard to to half screen landscape mode in Android ? 2 Answers

When I hold down the space bar, I want a meter to fill up but it wont stop,I am trying to make a meter increase while the space key is held down but it wont stop increasing. 0 Answers

Keyboard on Unity 0 Answers

Input field keyboard not showing up after building it on android 0 Answers

flushinp() for unity? 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