Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 toby · Jul 26, 2011 at 08:37 AM · movementfpssliderscrollbar

Slider for FPS Movement Speed

Hello guys, 1 problem occur with my slider is when i trying to interrupt with the bar (left and right) and nothing change in CharacterMotor's speed value. Is that any code I'm missing in the MoveSlider() ?

here the script:

/ Label and Slider Compound Control /

var Amount: int = 5; var maxForwardSpeed; var maxSidewaysSpeed; var maxBackwardsSpeed;

function OnGUI () {

//movement speed

 if (GUI.Button(Rect(3, 670, 20, 20), "-"))
 {
 gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed-1;
 gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed-1;
 gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed-1;
 Amount--;
 }
 
 Amount = GUI.HorizontalScrollbar (Rect(28, 670, 100, 20), Amount, 5, 0, 600);
 
 if (GUI.Button(Rect(133, 670, 20, 20), "+"))
 {
 gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed+1;
 gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed+1;
 gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed+1;
 Amount++;
 }
 GUI.TextField (Rect(28, 690, 100, 20), String.Format ("{0:#.##}", gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed));

}

function MoveSlider() {

 if (Amount--) { 
 gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed-1;
 gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed-1;
 gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed-1;
   }
 
 if (Amount++) {
 gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed+1;
 gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed+1;
 gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
 gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed+1;
 }

}

Thanks, I'm newbie.

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 Rmaniac · Jul 29, 2011 at 07:39 PM

if (Amount--) and if (Amount++) will always be evaluated as TRUE since Amount is an int (which does not evaluate to 0 or 1) instead of a boolean, so both if blocks are being called when MoveSlider() is called.

Try setting a flag for when your movement is changing to evaluate in your if block. Just to make it easy, I'm using an int in the example below that will always be either 1 or 0.

 var int MoveSliderDown = 0; // starts as equivalent of where Amount-- was
 //..other code declarations...
 
 function OnGUI () {
   //movement speed
 
   if (GUI.Button(Rect(3, 670, 20, 20), "-")) {
       gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
       gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed-1;
       gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
       gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed-1;
       gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
       gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed-1;
       Amount--;
       MoveSliderDown = 0;
   }
   //<snip> 
   if (GUI.Button(Rect(133, 670, 20, 20), "+"))
   {
      gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed+1;
      gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed+1;
      gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed+1;
      Amount++;
      MoveSliderDown = 1;
 
 }
 
 function MoveSlider() {
 
    if (MoveSliderDown == 0)  {
      gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed-1;
      gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed-1;
      gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed-1;
    }
 
    if (MoveSliderDown == 1)  {
      gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed+1;
      gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed+1;
      gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed+1;
    }
 }

It would be better to use a boolean flag, but my brain's not working correctly right now :)

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 toby · Aug 01, 2011 at 09:06 AM 0
Share

i guess so...hehe by the way, the horizontalbar i'v done, appear only when user press "+" or "-"..

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

Cant move FPS controller 1 Answer

Using a Gui slider to rotate the character 1 Answer

How to make object fallow the mouse ?? 1 Answer

Can I change movement from Unity 3d into Unity 3D iOS 2 Answers

UnityScript How to Detect Movement from FPS Controller 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