• 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 colsonenderby · Jul 18, 2011 at 05:58 AM · guispeed upboost

I need Help With a Speed up script

I want my character to have a speed up boost when ever he inputs left shift but it only lasts for a few seconds and cant be used again for 10 seconds. There is also a Gui texture that is supposed to appear when its ready to be used.

Here is the script i used but the Gui texture does not work right.

var speed= 40.0; var rotateSpeed = 3.0; var runningSpeed= 10.0; var strafeSpeed = 40; var savedTime=0; var GUI1 : GUITexture;

function Update () {

     var seconds : int = Time.time;
     var oddeven = (seconds % 2);
         
     if(oddeven)
     {
         Shoot(seconds);
     }

}

function Start(){ GUI1.enabled = false; }

function Shoot(seconds){

     GUI1.enabled = true;

if (Input.GetKey("left shift")){ if(seconds!=savedTime){ var controller : CharacterController = GetComponent(CharacterController); //transform.Rotate(0, Input.GetAxis ("Horizontal") rotateSpeed, 0); var forward = transform.TransformDirection(Vector3.forward); var curSpeed = speed Input.GetAxis ("Vertical"); controller.SimpleMove(forward * curSpeed); } } }

Comment
Add comment · Show 1
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 GuyTidhar · Jul 18, 2011 at 06:30 AM 0
Share

Please make sure you format all code snippets using the button labeled: "101010"

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GuyTidhar · Jul 18, 2011 at 06:29 AM

Here is an option:

 var speed : float = 40.0; 
 var rotateSpeed : float = 3.0; 
 var runningSpeed : float = 10.0; 
 var strafeSpeed : float = 40; 
 var GUI1 : GUITexture;
 
 var speedUpTime : float = 5; // How long do you have a speed boost
 var speedUpFactor : float = 1;
 var controller : CharacterController; 
 
 function Start()
 { 
     controller = GetComponent(CharacterController); 
      GUI1.enabled = true; 
 }
 
 function Update () 
 {
     if ( GUI1.enabled && Input.GetKey("left shift") )
     {
         StartCoroutine(EnableShiftAfterAWhile());
         StartCoroutine(SpeedUpForAWhile());
     }
 
     // Put here you movement processing
     transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0); 
     var forward = transform.TransformDirection(Vector3.forward); 
     // You current speed should be effected by the speedFactor
     var curSpeed = speed * Input.GetAxis ("Vertical") * speedUpFactor; 
     controller.SimpleMove(forward * curSpeed); 
 }
 
 function SpeedUpForAWhile()
 {
     speedUpFactor = 2; // Move twice as fast
 
     yield WaitForSeconds(speedUpTime);
 
     speedUpFactor = 1; // Regular move speed
 }
 
 function EnableShiftAfterAWhile()
 {
     GUI1.enabled = false;
 
     yield WaitForSeconds(10);
 
     // Once the time wait time has passed you can re-enable the gui (and shift...)
 
     GUI1.enabled = true;
 }


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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUI Sliders in the same script. Question (Js.) 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Need help with my script 0 Answers

GUI.HorizontalSlider not working 2 Answers

Setting Scroll View Width GUILayout 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges