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 Orggrim · Mar 08, 2012 at 12:15 AM · speedforceballadd

Add Force: Increase Speed of Ball periodically

Hi, I am making a 4 player pong game, and I want the ball to increase speed every 10 seconds. heres the code i have so far, im just not sure what to add to it to achieve this.

 var temp : Vector3;
 
 
 function Start() 
 {
     
     gameObject.rigidbody.AddForce(500,0,500);
  }
 
 function OnCollisionEnter(col : Collision)
 {
     temp = gameObject.rigidbody.Velocity;
     Debug.Log("X: " + temp.x + " Y: " + temp.y);
     if(temp.x > 0)
     {
         gameObject.rigidbody.AddForce(-1000, 0, 0);
     }
     else if(temp.x <0)
     {
         gameObject.rigidbody.AddForce(1000, 0, 0);
     }
     if(temp.z > 0)
     {
         gameObject.rigidbody.AddForce(0, 0, -1000);
     }
     else if(temp.z <0)
     {
         gameObject.rigidbody.AddForce(0, 0, 1000);
     }
 }
 
 
 function Update()
 {
 var temp : Vector3 = gameObject.rigidbody.Velocity;
     if(temp.x > 0)
     {
         gameObject.rigidbody.AddForce(-1500, 0, 0);
     }
     else if(temp.x <0)
     {
         gameObject.rigidbody.AddForce(1500, 0, 0);
     }
     if(temp.z > 0)
     {
         gameObject.rigidbody.AddForce(0, 0, -1500);
     }
     else if(temp.z <0)
     {
         gameObject.rigidbody.AddForce(0, 0, 1500);
     }
 }
     enter code here
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 DGArtistsInc · Mar 08, 2012 at 03:30 AM 0
Share

well ins$$anonymous$$d of just using numbers i would create speed variables and have those add up over time.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by syclamoth · Mar 08, 2012 at 04:27 AM

Instead of doing that super-convoluted thing there, why not just use something like this?

 rigidbody.AddForce(rigidbody.velocity.normalized * forceAmount, ForceMode.Impulse);

This will add force based on the current velocity of the object. Otherwise, just rely on the physics engine for collisions and reflections.

Comment
Add comment · Show 2 · 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 Orggrim · Mar 08, 2012 at 10:04 AM 0
Share

by "super-convoluted" do u mean the code under function Update?

avatar image syclamoth · Mar 28, 2012 at 12:10 AM 0
Share

No, I mean all of it. The code you are using to increase the speed of the ball is overcomplicated and error-prone.

avatar image
1

Answer by VIPINSIRWANI · Jul 12, 2013 at 12:04 PM

Set one Variable time and take Time.time after every 10 sec set it like

rigidbody.velocity = new Vector3(rigidbody.velocity.x speed, 0, rigidbody.velocity.z speed)

May Be it will work once try...

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 tdaniels · Mar 08, 2012 at 04:30 AM

If you're having issue with the time, set up a time variable and add ten seconds to it. Whenever Time.time is greater than your time variable, increase the velocity by whatever and reset your time variable by 10 seconds. That or do an ienumerator function.

If you're having issue with adding force I'd create a speed variable and use that to drive the movement. Someone else can probably give you more exact instructions for that.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make an object move forward on its own 2 Answers

My object's direction angle changes after hit a collider in a certain velocity, but also not change in different speeds. 0 Answers

Help with player movement and adding force to a ball. 1 Answer

Limiting Speed on Two Axis 1 Answer

Precalculate force before applying it. 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