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 eriger777 · Aug 23, 2012 at 08:23 PM · soundspaceshipdrivewarp

Warp drive and sound on key press

I'm trying to make a warp jump for my space game. Meaning change the speed of the space ship. I'm using a space starer kit here's the script that moves the ship. I also want it to play a song when the key (J) is pressed and change the ship speed. using UnityEngine; using System.Collections.Generic;

 [RequireComponent(typeof(Spaceship))]
 [AddComponentMenu("Game/Spaceship Controller")]
 public class SpaceshipController : MonoBehaviour
 {
     public enum InputType
     {
         Mouse,
         Controller,
         Touchpad,
     }
 
     public bool movement = true;
     public bool weapons = true;
 
     Spaceship mShip;
     Vector3 mDelta;
     int mSteeringTouch = -1;
     float mNextPrimary = 0f;
     float mNextSecondary = 0f;
 
     List<Weapon> mPrimary = new List<Weapon>();
     List<Weapon> mSecondary = new List<Weapon>();
 
     /// <summary>
     /// Determine whether we own this ship, input type, and ensure we have a network observer.
     /// </summary>
     
     void Start()
     {
         // If we don't control the ship, this script isn't needed
         if (NetworkManager.isConnected && !NetworkManager.IsMine(this))
         {
             Destroy(this);
             return;
         }
 
         // We need to know the ship we're controlling.
         mShip = GetComponent<Spaceship>();
 
         // Find all weapons
         Weapon[] weapons = GetComponentsInChildren<Weapon>();
         
         foreach (Weapon w in weapons)
         {
             if (w.group == Weapon.Group.Primary) mPrimary.Add(w);
             if (w.group == Weapon.Group.Secondary) mSecondary.Add(w);
         }
     }
     
     /// <summary>
     /// Process input if we're controlling this ship.
     /// </summary>
 
     void Update()
     {
         Vector3 turn = mShip.turningInput;
         Vector3 move = mShip.moveInput;
 
         if (ControlScheme.uiMode)
         {
             // UI mode -- ignore input
             mDelta = Vector2.zero;
         }
         else
         {
             // Get the movement axes
             if (ControlScheme.current == ControlScheme.InputSource.Touchpad)
             {
                 Vector3 touchMove = Vector3.zero;
                 Vector3 touchTurn = Vector3.zero;
 
                 foreach (Touch touch in Input.touches)
                 {
                     // Ignore the touch used to steer
                     if (mSteeringTouch == touch.fingerId)
                     {
                         if (touch.phase == TouchPhase.Moved)
                         {
                             mDelta.x += touch.deltaPosition.x;
                             mDelta.y += touch.deltaPosition.y;
                         }
                         else if (touch.phase == TouchPhase.Ended ||
                                  touch.phase == TouchPhase.Canceled)
                         {
                             mDelta = Vector2.zero;
                             mSteeringTouch = -1;
                         }
                     }
                     else if (touch.position.x < Screen.width * 0.25f)
                     {
                         // Bottom part of the screen
                         if (touch.position.y < Screen.height * 0.25f)
                         {
                             // Bottom-left corner -- slow down
                             touchMove.z = -1f;
                         }
                         else if (touch.position.y > Screen.height * 0.75f)
                         {
                             // Top-left corner -- accelerate
                             touchMove.z = 1f;
                         }
                         else
                         {
                             // Left side -- roll left
                             touchTurn.z = -1f;
                         }
                     }
                     else if (touch.position.x > Screen.width * 0.75f)
                     {
                         if (touch.position.y < Screen.height * 0.25f)
                         {
                             // Bottom right corner -- shoot primary weapon
                             if (weapons && mPrimary.Count > 0 && mNextPrimary < Time.time)
                             {
                                 mNextPrimary = Fire(mPrimary);
                             }
                         }
                         else if (touch.position.y > Screen.height * 0.75f)
                         {
                             // Top right corner -- shoot secondary weapon
                             if (weapons && mSecondary.Count > 0 && mNextSecondary < Time.time)
                             {
                                 mNextSecondary = Fire(mSecondary);
                             }
                         }
                         else
                         {
                             // Right side -- roll right
                             touchTurn.z = 1f;
                         }
                     }
                     else if (touch.position.x > Screen.width * 0.25f &&
                              touch.position.x < Screen.width * 0.75f &&
                              touch.position.y > Screen.width * 0.25f &&
                              touch.position.y < Screen.width * 0.75f)
                     {
                         // Central part of the screen
                         if (touch.phase == TouchPhase.Began)
                         {
                             mSteeringTouch = touch.fingerId;
                         }
                     }
                 }
 
                 float maxDist = 1.0f / Mathf.Min(Screen.width * 0.4f, Screen.height * 0.4f);
 
                 touchTurn.x = Mathf.Clamp(mDelta.y * maxDist, -1f, 1f);
                 touchTurn.y = Mathf.Clamp(mDelta.x * maxDist, -1f, 1f);
 
                 move = touchMove;
                 turn = touchTurn;
             }
             else
             {
                 if (movement)
                 {
                     move.x = Input.GetAxis("Right");
                     move.y = Input.GetAxis("Up");
                     move.z = Input.GetAxis("Forward");
                     move.z = Input.GetAxis("Forward");
                     // Record the roll rotation
                     turn.z = Input.GetAxis("Roll");
 
                     if (ControlScheme.current == ControlScheme.InputSource.Controller)
                     {
                         if (!Application.isEditor) Screen.lockCursor = false;
 
                         // Joystick-based control
                         turn.x = Input.GetAxis("Look Up");
                         turn.y = Input.GetAxis("Look Right");
 
                         mDelta = Vector2.zero;
                     }
                     else
                     {
                         if (!Application.isEditor) Screen.lockCursor = true;
 
                         // Mouse-based control
                         mDelta.x += Input.GetAxis("Mouse X");
                         mDelta.y += Input.GetAxis("Mouse Y");
 
                         float maxDist = 1.0f / Mathf.Min(Screen.width, Screen.height);
 
                         turn.x = Mathf.Clamp(mDelta.y * maxDist, -1f, 1f);
                         turn.y = Mathf.Clamp(mDelta.x * maxDist, -1f, 1f);
                     }
                 }
 
                 // Fire the primary weapon
                 if (weapons)
                 {
                     if (mPrimary.Count > 0 && (Input.GetMouseButton(0) || Input.GetKey(KeyCode.JoystickButton4)))
                     {
                         if (mNextPrimary < Time.time)
                         {
                             mNextPrimary = Fire(mPrimary);
                         }
                     }
 
                     // Fire the secondary weapon
                     if (mSecondary.Count > 0 && (Input.GetMouseButton(1) || Input.GetKey(KeyCode.JoystickButton5)))
                     {
                         if (mNextSecondary < Time.time)
                         {
                             mNextSecondary = Fire(mSecondary);
                         }
                     }
                 }
             }
         }
 
         // Backwards movement should act as an anchor and should negate all other movement
         if (move.z < 0f)
         {
             // Drain power while the "brake" button is held
             if (mShip.powerGenerator != null)
             {
                 float mult = Time.deltaTime * 30f;
                 move.z = -1.0f + mShip.powerGenerator.DrainPower(-move.z * mult) / mult;
             }
 
             move.x *= 1f + move.z;
             move.y *= 1f + move.z;
         }
 
         // Ensure the vector is unit length
         float mag = move.magnitude;
         if (mag > 1f) move *= 1.0f / mag;
 
         // Ensure the vector is unit length
         mag = turn.magnitude;
 
         if (mag > 1f)
         {
             turn *= 1.0f / mag;
             mag = 1f;
         }
 
         // Sensitivity tweak to make it easier to snipe-aim
         turn *= Tools.Ramp(mag, 0.35f);
 
         // Set the ship's values
         mShip.turningInput = turn;
         mShip.moveInput = move;
     }
 
     /// <summary>
     /// Helper function used above. Fires a weapon from the specified list and returns the next time we'll be able to fire.
     /// </summary>
 
     static float Fire (List<Weapon> list)
     {
         foreach (Weapon wp in list)
         {
             if (wp.canFire)
             {
                 wp.Fire();
                 return Time.time + wp.firedObject.firingFrequency / (float)list.Count;
             }
         }
         return Time.time;
     }
 }
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

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

7 People are following this question.

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

Related Questions

Good prefab/assest to buy for FPS style shooter iOS? 0 Answers

How do you make an orbit enemy ai? 0 Answers

Find or make sound effects. 2 Answers

High Speed 1 Answer

Camera Shaking At High Speed 1 Answer


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