• 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
Question by TargonStudios · Nov 03, 2012 at 03:45 PM · variablebooleanflashlight

Running faster while flashlight is off

So I have created a flashlight where it can be turned on and off (pressing q) But I want my player to be able to run faster while the flashlight is turned off. any ideas?

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Kiloblargh · Nov 03, 2012 at 04:50 PM

 if (flashlight.active)
     {
 maxSpeed = 1;
     }
 else
     {
    maxSpeed = 2;
     }
Comment

People who like this

0 Show 7 · 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 TargonStudios · Nov 04, 2012 at 12:08 AM 0
Share

Hmm, how do i use it? heres my script for a flashlight, but i don't know how to add yours to make my player run faster.

 function Update () {

if (Input.GetKeyDown("q")){

if (light.enabled == true) light.enabled = false; else

 light.enabled = true;
   
 }
 

}

avatar image Coreyf716 · Nov 04, 2012 at 12:52 AM 0
Share

You need a script for player movement. Create a static variable of maxSpeed within it.

Rather than using 'if (light.active == true)', use 'if (light.active)'. They mean the same thing.

avatar image TargonStudios · Nov 04, 2012 at 01:18 AM 0
Share

like this?

 static var  maxspeed;
 function Update () {

if (Input.GetKeyDown("q")){

if (light.enabled == true) light.enabled = false; else

 light.enabled = true;
 
 if (light.active)
 {

maxSpeed = 1; } else { maxSpeed = 2; }

 }
 

}

avatar image Althaen · Nov 04, 2012 at 01:25 AM 0
Share

You have to set that boolean as a variable outside of Update. Always set it false.

var lightOn = false;

avatar image TargonStudios · Nov 04, 2012 at 01:38 AM 0
Share

I stink at scripting.... I'm so confused...

Show more comments
avatar image

Answer by Althaen · Nov 04, 2012 at 04:12 PM

I think this should work.

 static var maxSpeed = 0;
 private var lightOn = false;
 
 Update () {
     if (Input.GetKeyDown(KeyCode.Q)){
         if(lightOn==false){
             lightOn = true;
             maxSpeed = 1;
         }
         else{
             lightOff = false
             maxSpeed = 2;
         }
     }
 }
Comment

People who like this

0 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 Coreyf716 · Nov 04, 2012 at 09:08 PM 0
Share

It's a good idea to define your variable type. Also, booleans default to false.

avatar image

Answer by Coreyf716 · Nov 04, 2012 at 02:50 PM

Now, use this code for Move.js...

 #pragma strict
 
 static var maxSpeed : float;
 public var player : CharacterController;
 public var mouseSensitivityX : float;
 private var maxSpeedString : String;
 
 function Start () { }
 
 function Update () {
     maxSpeedString = String.Format ("Max Speed : {0}", Move.maxSpeed.ToString());
     transform.Rotate (0, Input.GetAxis ("Mouse X") * mouseSensitivityX * Time.deltaTime, 0);
     if (Input.GetKey (KeyCode.W)) {
         player.Move (transform.TransformDirection (Vector3.forward) * Move.maxSpeed * Time.deltaTime);
     }
 }
 
 function OnGUI () {
     (GUI.Label (Rect (25, 25, 100, 75), maxSpeedString));
 }

...and for Flashlight.js

 #pragma strict
 
 public var flashlight : Light;
 public var isLightOn : boolean;
 
 function Start () { }
 
 function Update () {
     if (Input.GetKeyDown (KeyCode.Q))
         isLightOn = !isLightOn;    
     if (isLightOn) {
         flashlight.enabled = true;
         Move.maxSpeed = 5;
     } else {
         flashlight.enabled = false;
         Move.maxSpeed = 8;
     }
 }
 

I've added another script too...

 #pragma strict
 
 public var mouseSensitivityY : float;
 
 function Start () { }
 
 function Update () {
     transform.Rotate (Input.GetAxis ("Mouse Y") * mouseSensitivityY * Time.deltaTime, 0, 0);
 }

I converted maxSpeed into a string so you can see how fast you are going.

This is the link to the updated project...

https://www.dropbox.com/sh/pys91q8qz6grbgp/QxHLXhWS4g

Comment

People who like this

0 Show 16 · 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 Coreyf716 · Nov 04, 2012 at 02:54 PM 0
Share

The link is to the project. You can export the scripts into your project. Let me know if you have any questions. Good luck! :)

I added line of code that allows your player to look around. The code has been tested. It all works now.

avatar image TargonStudios · Nov 04, 2012 at 02:55 PM 0
Share

Ill try it in a bit, im working on another thing for it right now

avatar image Coreyf716 · Nov 04, 2012 at 02:58 PM 0
Share

Ok. Tell me what you think of the project. It will explain everything.

avatar image TargonStudios · Nov 04, 2012 at 03:04 PM 0
Share

Flashlight: Assets/Flashlight.js(15,8): BCE0005: Unknown identifier: 'Move'.

avatar image Coreyf716 · Nov 04, 2012 at 03:10 PM 0
Share

You must name the movement script "Move". Also, make sure that maxSpeed is a static variable.

Show more comments

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Pass bool value from multiple objects to one file to check their values? 1 Answer

Unity says there is no such thing as a boolean. 1 Answer

can't call script variable from another script 1 Answer

Referencing a variable from another script by another variable 3 Answers

how to create boolean Varibles ? 3 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