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 ThePossessedWaffle · Dec 24, 2013 at 12:34 AM · ballstuck

How do i stop a ball from getting stuck?

Hey guys, im making a game like pong but sometimes when im testing it the ball gets stuck by that i mean it keeps bouncing up and down in the exact same spot is there any way to prevent this from happening here is my script

 #pragma strict
 
 var cSpeed:float = 10.0;
 var sFactor:float = 10.0;
 static var playerScore:int = 0;
 static var enemyScore:int = 0;
 
 function Start () 
 {
     rigidbody.AddForce(10,0,0);
 }
 
 function Update () 
 {
     var cvel = rigidbody.velocity;
     var tvel = cvel.normalized * cSpeed;
     rigidbody.velocity = Vector3.Lerp(cvel,tvel,Time.deltaTime * sFactor);
     
     if(transform.position.x > 23)
     {
         playerScore++;
         transform.position.x = 0;
         transform.position.y = 0;
     }
     if(transform.position.x < -23)
     {
         enemyScore++;
         transform.position.x = 0;
         transform.position.y = 0;
     }
 }
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
0

Answer by drulludanni · Dec 24, 2013 at 12:41 AM

depending on which axis is the one where it gets stuck just be sure that the value is never 0 and if it is 0 just give it little nudge. for example if it gets stuck bouncing along the X-axis while Y-axis is 0 then add like 0.5 to the Y-axis movement

Edit: up on looking at your code I see that it would get stuck when the X- axis movement is 0 so you would have to add some movement to the X axis not vice versa

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 robertbu · Dec 24, 2013 at 12:46 AM

As a hack you can do something like this:

 var dot = Vector3.Dot(rigidbody.velocity.normalized, Vector3.up);
 if (dot > 0.98 || dot < -0.98) {
     var v = Random.insideUnitCircle * factor;
     rigidbody.velocity += v;
 }

This detects when the ball movement is nearly vertical and then introduces a bit of random change. 'factor' is a variable you declare or constant you insert. You'll have to play to get the value right. This could be put inside FixedUpdate(), but the random movement would be better hidden if you were to only make the change during a collision (i.e. inside OnCollisionEnter() OR OnCollisionExit()).

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 sparkzbarca · Dec 24, 2013 at 12:51 AM

rigidbody.velocity = Vector3.Lerp(cvel,tvel,Time.deltaTime * sFactor);

dont do this

rigidbodies are for physics calculations.

Velocity should be modified using addforce not directly.

basically this is the issue in a nutshell

  1. adding a rigidbody to something means you want it to obey the laws of physics

  2. velocity is the speed of something in each direction.

  3. velocity when changed directly is an instantaneous change, so velocity = 500 or 0 means it instantly goes really fast or stops completely in 1 frame with no speeding/slowing just an instant change.

  4. this change completely violates physics, if your going to do that modify the transform directly don't use a physics component to do it.

so use

rigidbody.addforce(direction * force/speed)

to add force because then it will do it in a realistic way. it'll use physics equations to figure out what velocity should be after applying force. For example by you doing velocity = X in an update loop, your basically ignoring gravity, gravity will be applied but then by going trhough and messing with velocity every frame your undoing it, so its doing all these calcs and then changing velocity and applying the resuult and after that you just toss it out and do it yourself.

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 Chambers88 · Dec 08, 2020 at 12:50 AM 0
Share

The collision detection that comes with Unity's physics system is useful even if you are not simulating realistic physics. You can make a game with "game physics" such as Super $$anonymous$$ario Bros and make great use of Unity's Physics2D for the job, even if you set the object's velocity at some times.

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

21 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

Related Questions

how perevent ball from stuck in corner ? 0 Answers

Rolling ball gets stuck in corners 1 Answer

Ball getting stuck on going sideways. 1 Answer

Sphere casting a wall always returns false? 1 Answer

Function can't be called 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