Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by astericaewrites · Nov 02, 2021 at 03:42 PM · unity 2dspeed

How to have an object stop incrementing speed after a certain value?,

So I'm playing around with making a simple Pong game (with tutorials), and am trying to have it so the ball doesn't move any faster after a certain point (it gets a bit faster each time it hits a paddle). Because if it moves too fast it just goes right through the paddles. I had wanted to add a couple extra features to make it my own even though I'm still new with coding.

Any help is greatly appreciated!

 public class Ball : MonoBehaviour
 {
     public float speed = 200.0f;
     private Rigidbody2D _rigidbody;
 
     private void Awake()
     {
         _rigidbody = GetComponent<Rigidbody2D>();
     }
 
     private void Start ()
     {
         resetPosition();
         AddStartingForce();
     }
 
     public void resetPosition()
     {
         _rigidbody.position = Vector2.zero;
         _rigidbody.velocity = Vector2.zero;
     }
 
     public void AddStartingForce()
     {
         float x = Random.value < 0.5f ? -1.0f : 1.0f;
         float y = Random.value < 0.5f ? Random.Range(-1.0f, -0.5f) : Random.Range(0.5f, 1.0f);
 
         Vector2 direction = new Vector2(x, y);
         _rigidbody.AddForce(direction * this.speed);
     }
 
     public void AddForce(Vector2 force)
     {
         _rigidbody.AddForce(force);
     }
 
 }
 
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
1

Answer by swanne · Nov 02, 2021 at 03:57 PM

Apologies, I have done some testing and found my mistake.
On the ball use this

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Ball : MonoBehaviour
 {
 
     public float moveSpeed = 1f;
     public float maxSpeed = 4;
 
     Rigidbody rb;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         Move();
     }
 
     void Move()
     {
         rb.AddForce(Vector3.right * moveSpeed);
     }
 
     private void Update()
     {
         Debug.Log(rb.velocity.magnitude);
 
         
         if(rb.velocity.magnitude > maxSpeed)
         {
             rb.velocity = rb.velocity.normalized * maxSpeed;
         }
         
     }
 }



Then on each of the paddles use this:
using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Paddle : MonoBehaviour
 {
 
     Rigidbody rb;
     public float force = 50f;
 
     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "Ball") // Do not forget assign tag to the field
         {
             rb = col.gameObject.GetComponent<Rigidbody>();
             rb.AddForce(-transform.right * force);
         }
     }
 }
 



On the left paddle, enter a negative value and it'll make the ball travel right. eg. -300

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 astericaewrites · Nov 02, 2021 at 08:07 PM 0
Share

I'm unfortunately getting an error though.. alt text

And this is what I added:

 public class Ball : MonoBehaviour
 {
   
     public float maxSpeed = 250.0f;
 
 
 
     private void FixedUpdate()
     {
         if (_rigidbody.velocity > maxSpeed)
         {
             _rigidbody.velocity = maxSpeed;
         }
     }
 
 }

avatar image swanne astericaewrites · Nov 02, 2021 at 08:49 PM 0
Share

Hey @astericaewrites

I made a short video to better explain my answer

https://youtu.be/IZsgpeNm-Ds

avatar image astericaewrites swanne · Nov 03, 2021 at 01:30 AM 0
Share

Thank you very much for all your help! It has been very useful :)

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

138 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 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 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 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 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

Finding the speed of MoveTowards 1 Answer

increase speed every spawn wave 0 Answers

Putting a brakes on my spaceship 0 Answers

Hi, knife hit, how do you control the speed of the wheel and the speed of the knife? 1 Answer

Making Top-Down spaceship movement, getting current speed, acceleration, without rigidbody! 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