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 UNDERHILL · Jul 17, 2014 at 09:56 AM · constantforce

ConstantForce not changing?

I have an object onto which by script this script is loaded.

I can see the value change once but it doesn't change again. It's like constantForce is very slow to update? or doesn't accept updates? tried constantForce.force and constantForce.relativeForce both don't seem to accept changes? Also tried this.gameObject.constantForce.etc? tried it also in update and fixedupdate AND ALSO i tried it with and without "new"

I can see the debugs firing but the values don't change.

 #pragma strict
 
 var FUloops : int = 0;
 
 function Update()
 {
     if (FUloops == 1000)
     {
         FUloops = 0;
     }
     FUloops++;
         
     if (FUloops < 500)
     {    
         constantForce.relativeForce = new Vector3(0,0,-10);
         Debug.Log("less than");
     }
     else
     {
         constantForce.relativeForce = new Vector3(0,0,10);
         Debug.Log("not less than");
     }
 }

I hope I'm missing something dumb??????

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
Best Answer

Answer by Saad_Khawaja · Jul 17, 2014 at 10:30 AM

Yes, there are a number of things wrong.

Firstly, you should never use int in update to compare or add delay (i'm assuming you did that to add delay). The reason is that Update is called on every frame so if you have fps as 100, it will be a different delay than a device on which you have fps 400 per say.

You can use coroutines or Time.deltatime to make sure that the delay is frame rate independent.

Secondly, your code changes constant force however, once the object is falling, it has a lot of force. So you need to cancel that force before adding force in the opposite direction.

Try this code: It is tested and works.

 #pragma strict
 var timeSum:float=0;
 var currentDirection:int=0;
 
 function addConstantForce(direction: int)
 {
         gameObject.rigidbody.velocity = Vector3.zero;
          constantForce.relativeForce = new Vector3(0,direction*10,0);
 }
 
 
 function Update()
 {
 
     if (timeSum >= 2f)
     {
         timeSum = 0f;
     }
     
     timeSum+=Time.deltaTime;
  
     if (timeSum < 1f)
     {
         
         if(currentDirection != -1)
             addConstantForce(-1);
             
         currentDirection = -1;
     }
     else
     {
          if(currentDirection != 1)
               addConstantForce(1);
                     
          currentDirection = 1;
     }
     
 
 }

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Adding a constant force to a barrel when i shoot it (some kind of high pressure that is eliberated) 3 Answers

Using ConstantForce to simulate wind 0 Answers

Constant Force Script Changing 1 Answer

GetComponent ().force = -Vector3.MoveTowards( transform.position, Vector3.zero, Time.deltaTime*speed); 1 Answer

Creating false gravity for a ball 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