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 /
  • Help Room /
This question was closed Sep 13, 2015 at 09:39 PM by OldRoutes for the following reason:

I found the mistake in my code - I needed to set 'launchBall' to false in the FixedUpdate function. Woops.

avatar image
0
Question by OldRoutes · Sep 13, 2015 at 07:15 PM · rigidbodyupdatefixedupdate

Breakout Game Tutorial Question - Putting addForce in FixedUpdate makes the ball fly out of the screen at high speed?

Hey guys!

After completing this tutorial : https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/creating-a-breakout-game , I've tried to re-implement the physics of the ball in FixedUpdate(), and the ball seems to behave quite differently than the implementation in the tutorial.

I'm really interested to find out why... Here's my code for the ball GameObject, for example :

 using UnityEngine;
 using System.Collections;
 
 public class Ball : MonoBehaviour {
     
     public float ballInitialVelocity = 600f;
     private Rigidbody rb;
     private Vector3 paddlePos;
 
     private float ballVelocity;
     private float chargeRate = 90f;
     private bool charging = false;
     private bool launchBall = false;
     private Vector3 mousePosition;
     
     void Awake () {
         
         rb = GetComponent<Rigidbody>();
     }
     
     void Update () 
     {
 
         if (Input.GetButtonDown("Fire1") && GM.instance.ballInPlay == false)
         {
             ballVelocity = ballInitialVelocity;
             //Mathf.Clamp(ballVelocity, 0, 1000f)
             charging = true;
         }
         if (charging == true)
         {
             print("Before : " + ballVelocity);
             ballVelocity = Mathf.Clamp(ballVelocity + (chargeRate * Time.deltaTime), 0, 800f);
             print("After : " + ballVelocity);
         }
         if (Input.GetButtonUp("Fire1") && charging == true){
             launchBall = true;
 
         }
     }
 
     void FixedUpdate()
     {
         // Launch the ball
         if (launchBall == true)
         {
             charging = false;
             transform.parent = null;
             GM.instance.ballInPlay = true;
             rb.isKinematic = false;
             rb.AddForce(new Vector3(ballVelocity, ballVelocity, 0));
             ballVelocity = ballInitialVelocity;
         }
     }
 }
 

I can't really figure out why it's behaving so differently than when it's implemented within Update()... any ideas?

Sorry if that seems obvious ; I've tried looking for an answer and I couldn't find anything in recent questions.

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

  • Sort: 
avatar image
0

Answer by hexagonius · Sep 13, 2015 at 08:21 PM

That's probably because you use a velocity vector calculated with Time.DeltaTime from Update. FixedUpdate uses that automatically (see the example in the Unity Script Reference for AddForce).

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 OldRoutes · Sep 13, 2015 at 08:59 PM 0
Share

Thanks for the answer! I've looked at the example, but I'm not entirely sure what you mean by "does that automatically"?

avatar image hexagonius · Sep 14, 2015 at 05:29 AM 0
Share

your calculation:

 ballVelocity = $$anonymous$$athf.Clamp(ballVelocity + (chargeRate * Time.deltaTime), 0, 800f);

needed for FixedUpdate:

 ballVelocity = $$anonymous$$athf.Clamp(ballVelocity + chargeRate, 0, 800f);

Follow this Question

Answers Answers and Comments

28 People are following this question.

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

Related Questions

Physics after build feel different vs In-Editor (Time.deltaTime) 1 Answer

Make FixedUpdate camera be static like in Update? 0 Answers

Moving object in 'x' axis . should it be inside update or fixedupdate.? 0 Answers

Reduce framerate for server 1 Answer

Problem with DontGoThroughThings script 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