Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Silent_Eagle · Nov 21, 2016 at 10:10 PM · c#2dphysicsvelocityphysics2d

Is there a way to lock velocity?

Im making a 2d pong like game with powerups. one of the powerups doubles the velocity of the ball. Ive been having problems with the physics engine as when I hit the ball with the side of my paddle the ball speeds up alot. Is there any way to lock the velocity to a certain number unless the powerup is activated?`public Rigidbody2D rb; public float ballForce; public Rigidbody2D powerUpRB; private bool timerStart = false; private float timeLeft = 1f;

 // Use this for initialization
 void Start () {
     
 }

 // Update is called once per frame
 void Update()
 {
  


     if (Input.GetKeyUp(KeyCode.Space))
     {
         rb.velocity = new Vector2(ballForce, ballForce);
     }
 }


 void OnCollisionEnter2D(Collision2D coll)
 {
     if (coll.gameObject.name != "doubleBallSpeed") return;

     rb.velocity = new Vector2(ballForce * 2, ballForce * 2);
     StartCoroutine("DelayTime");
     Destroy(coll.gameObject);
 }

 IEnumerator DelayTime()
 {
     yield return new WaitForSeconds(5f);
     rb.velocity = new Vector2(ballForce / -2, ballForce / 2);
 }

}

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 Sergio7888 · Nov 22, 2016 at 01:41 AM 0
Share

You need execute Physic actions in FixedUpdate calling in Update will slow your code.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by ZeroSumGames · Nov 21, 2016 at 11:53 PM

Yes, there is. What you need to do is measure the Magnitude of the velocity. If it exceed the max speed, then you normalize the vector and multiply it by the max speed.

      Vector2 vel = new Vector2();
         float mag = vel.magnitude;
         if (mag >= MaxSpeed)
         {
             vel = vel.normalized * MaxSpeed;
         }
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 onurinaltest · Apr 09, 2021 at 10:32 AM 0
Share

Thank you so much <3 It helped me a lot.

avatar image
1

Answer by MattG54321 · Nov 22, 2016 at 08:35 AM

I recommend you try Mathf.Clamp(float value, float min, float max). It returns max if value is greater than max.

Example:

 float xVel = Mathf.clamp(ballForce * 2, minVel, maxVel);
 float yVel = Mathf.clamp(ballForce * 2, minVel, maxVel);
 rb.velocity = new Vector2(xVel, yVel);
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 romatallinn · Nov 21, 2016 at 10:47 PM

I am not sure what you mean by "lock the velocity to a certain number". If you want the velocity would not exceed some limits, you can use Mathf.Clamp. Or if you want to keep the speed the same all the time, then you just use Update function and assign there constant velocity.

Comment
Add comment · Show 4 · 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 Silent_Eagle · Nov 21, 2016 at 10:56 PM 0
Share

by lock I mean that the velocity would stay the same no matter what except when the powerup comes into effect. Is this what $$anonymous$$athf.Clamp would do?

avatar image romatallinn Silent_Eagle · Nov 21, 2016 at 10:58 PM 0
Share

@Silent_Eagle no. Then you just keep assigning velocity some constant value in update.

 void Update()
  {
          rb.velocity = new Vector2(ballForce, ballForce);
  }
avatar image Silent_Eagle romatallinn · Nov 21, 2016 at 11:01 PM 0
Share

will this fix whenever i slam the ball between the boundary and the paddle the ball speeds up. And as I want to eventually make this a mobile game with the players finger controlling the paddle I feel things could get out of hand.

Show more comments

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

10 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

Related Questions

Help with 2D physics script 1 Answer

How to rotate a GameObject with another GameObject while simulating gravity? 2 Answers

2d game platform physics 1 Answer

Physics in 2d arkanoid. Tangential and normal velocity 1 Answer

Velocity Movement & Physics Interactions by Rigidbody2D 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