• 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 xSpectrum · Sep 30, 2011 at 11:51 PM · c#float

Float value less than 1?

I'm making a game where the player has a jetpack. He clicks to use the jetpack. I'm doing this with this code, where "upasdf" is the player's y position and "jetpackSpeed" is a float.

 if (Input.GetKey(KeyCode.Mouse0))
         {
             
             for (float i = upasdf; i <= upasdf + jetpackSpeed; i++)
                 {
                 if (i <= 700)
                 GameObject.FindWithTag("Player").transform.position = new Vector3(x, i, z);
                 }
             
         
         }



The problem is that whenever I try to set "jetpackSpeed" to anything less than 1 (ie .5), it doesn't work. It doesn't give me an error, but it just won't make my player go up. How do I fix this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SilverTabby · Oct 01, 2011 at 12:20 AM

The section of code you gave here will do nothing if jetpackSpeed is less than 1 because the for loop's continue statement will not evaluate to true.

What's going on inside the computer:

 Start for loop
 assign:  i = currentPosition
 check :  currentPosition less than or equal to currentPosition + #?
 do body of loop
     the first time the loop is performed nothing happens (position = currentPosition)
 increment i
 check :  currentPosition + 1 less than or equal to currentPosition + #?
 if number < 1, then no it is not
 end loop

So that's why it doesn't work if # is less than 1. My question is Why are you using a loop?

Just do this:

DO NOT COPY-PASTE THIS CODE. TYPE IT OUT BY HAND AND MAKE SURE YOU UNDERSTAND EACH LINE BEFORE USING. Trust me, it helps.

 //At top of file
 var player : GameObject;
 var jetpackSpeed : Vector3 = Vector3(0, 5, 0);//half of gravity upwards per second
 
 //this is called once when the object turns on
 function Start()
 {
     //GameObject.Find is an expensive operation, also the player
     //   object will remain the same Object, usually.
     //   Find it once and keep track of it.
     player = GameObject.FindWithTag("Player");
 }
 
 //this function is called once every frame
 function Update()
 {
     //check if the user activated the jetpack
     if(Input.GetKey(KeyCode.Mouse0))
     {
         //move the player.   We multiplying by deltaTime will make it so that we
         //     always add jetpackSpeed once every second instead of once every frame
         player.transform.position += jetpackSpeed * Time.deltaTime;
     }
 
 }
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
avatar image
0

Answer by aldonaletto · Oct 01, 2011 at 12:46 AM

You're incrementing i in the for loop - it will become higher than upasdf+jetpackSpeed after one single loop.
Anyway, the whole idea is wrong - you are violating several Unity commandments:
1- thou shalt not stop Unity with a lengthy for loop (slows down your game);
2- thou shalt not move your character by setting position directly (collisions can't be detected);
3- thou shalt not use any Find function inside loops or Updates (your game will become as fast as a snail);

Since I don't know what is your character, I can suggest a simple way to do this jet pack effect - attach this simple script to the player:

public float jetpackSpeed = 2.0f; // speed in meters per second

void Update(){ if (Input.GetKey(KeyCode.Mouse0)){ // this will move the object up in a frame rate independent speed transform.Translate(0, jetpackSpeed * Time.deltaTime, 0, Space.World); } } This code still violates the second commandment, and no collisions will be detected. To solve this, I must know what is your character - CharacterController, rigidbody, or just a simple object.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Picking a Random Order for Levers 1 Answer

GetComponent C# 1 Answer

Adding coin score to multiplied value 2 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