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 siddharth3322 · Apr 21, 2015 at 12:42 PM · movementunity 2dconstantforcecontinuous

Move ball at constant speed on screen

I want to move ball constantly on screen as per my question title suggest. If your suggestion is using physics based movement then it is advantage for me.

Basically I am working on following game : Arkanoid

If you suggest me any link or tutorial for this then also it is okay, I will go over through that also. Please give your suggestion.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by lordlycastle · Apr 21, 2015 at 02:02 PM

Ah, a Breakout game, I created one too. There are many way to deal with movement e.g. rigidbody.AddForce, or rigidbody.velocity = …; and change you make to rigidbody are physics based to the engine will check if movement is possible before actually doing it. Here is a link to Unity’s breakout game tutorial, and I’m sure a Google search will give you many.

Below is a Ball class I used. I think my game was top-down, so you should change the axis accordingly.

 public class Ball : MonoBehaviour
 {
     public float MinimumSpeed = 25;
     public float MaximumSpeed = 30;
 
     //To prevent the ball from keep bouncing horizontally we enforce a minimum vertical movement
     public float MinimumVerticalMovement = 0.1F;
 
     //Don't move the ball unless you tell it to
     private bool hasBeenLaunched = false;
 
     void Update()
     {
         if (hasBeenLaunched)
         {
             //Get current speed and direction
             Vector3 direction = rigidbody.velocity;
             float speed = direction.magnitude;
             direction.Normalize();
 
             //Make sure the ball never goes straight horizotal else it could never come down to the paddle.
             if (direction.z > -MinimumVerticalMovement && direction.z < MinimumVerticalMovement)
             {
                 //Adjust the y, make sure it keeps going into the direction it was going (up or down)
                 direction.z = direction.z < 0 ? -MinimumVerticalMovement : MinimumVerticalMovement;
 
                 //Adjust the x also as x + y = 1
                 direction.x = direction.x < 0 ? -1 + MinimumVerticalMovement : 1 - MinimumVerticalMovement;
                 
                 //Apply it back to the ball
                 rigidbody.velocity = direction * speed;   
             }
 
             if (speed < MinimumSpeed || speed > MaximumSpeed)
             {
                 //Limit the speed so it always above min en below max
                 speed = Mathf.Clamp(speed, MinimumSpeed, MaximumSpeed);
 
                 //Apply the limit
                 //Note that we don't use * Time.deltaTime here since we set the velocity once, not every frame.
                 rigidbody.velocity = direction * speed;   
             }
         }
     }
 
     //When the bottom of the field it hit destroy the ball. 
     private void OnTriggerEnter(Collider other)
     {
         if (other.name == "Bottom")
         {
             Destroy(this.gameObject);
         }
     }
 
     public void Launch()
     {
         //Create a random vector but make sure it always point "up" (z axis in this case) else it could be launched straight down
         Vector3 randomDirection = new Vector3(Random.Range(-1.0F, 1.0F), 0, Mathf.Abs(Random.value));
 
         //Make sure we start at the minimum speed limit
         randomDirection = randomDirection.normalized * MinimumSpeed;
 
         //Apply it to the rigidbody so it keeps moving into that direction, untill it hits a block or wall
         rigidbody.velocity = randomDirection;
 
         hasBeenLaunched = true;
     }
 }


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 siddharth3322 · Apr 22, 2015 at 06:36 AM 0
Share

@lordlycastle, Thanks for your reply. Straight pasting of your code generate just horizontal ball movement in game. One more thing, I want only x and y movement of ball.

Please give some suggestion in this.

avatar image lordlycastle · Apr 22, 2015 at 08:37 AM 0
Share

@siddharth3322 Like I said, you’ll have to adjust the axis, as I made this top-down, and that is probably not you are doing. I did that by mistake, and never fixed it because I was just doing the project to learn. Don’t straight post the code, understand what’s happening and then apply to your project, yourself. The comments are pretty self-explanatory, it doesn’t just generate horizontal movement, that probably because you are viewing with x-y plane, while this makes the ball move in x-z.

avatar image
0

Answer by Vazent · Oct 25, 2017 at 02:45 PM

Your code helped me alot. Thank you !!

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 user6116 · Jan 26, 2018 at 06:34 PM

@lordlycastle After an hour of searching, your code got me to the answer i was looking for, thanks!

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 lordlycastle · Jan 26, 2018 at 08:02 PM 0
Share

Haha. Glad to be of assistance!

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

22 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

Related Questions

Unity 2d RigidBody Movement stutter 0 Answers

Hello everyone, I would like to create a Geometry dash like triangle Transporter in unity. 0 Answers

The game object penetrates the collision body when moving too fast 1 Answer

How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers

I want my vehicle to keep moving left/right while the buttons are held down? 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