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 Riskki · Aug 05, 2014 at 11:06 PM · bouncewallsgolf

How to stop ball bouncing in an upwards direction when hitting a wall

I have a mini golf like game that I'm making and I've created all the tracks on blender and imported then over to unity! now when my ball hits a wall sometimes it bounces off correctly but other times it bounces like normal but also raises on the y axis and basically jumps of the track which i don't want to happen, i want it to stay on the track at all times!

now I've checked the blender models I've imported over and the walls are perfectly vertical so no angle to push the ball upwards.

I've tried constraining the y axis but the ball seems to slow down really fast when hitting the wall after (i think this is because it takes the initial power i put on it and changes it to upward power, hence making it move slower)

ive also tried putting no friction on the walls and messing with the physics material but again no luck!

ive checked the forums and can't find anything so if anyone knows how i could keep the ball on the track at all times i would be very thankful.... i can't understand why the ball wants to move up at all but should stay horizontally with the course!

for any more details please ask

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 Mulldor · Aug 05, 2014 at 11:37 PM 0
Share

Its a hard one to answer without seeing your code!

You say you have tried constraining the Y axis, how?

Also, a very primitive solution you can do is to change the vector of the ball with every update.

I.e myBall = new Vector3(myBall.X, 1, myBallZ)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jix · Aug 06, 2014 at 12:22 AM

On the ball script add this code, just make sure that walls have the tag "Wall"

 void OnCollisionEnter(Collision other)
 {
     //Checking if the collision was with a wall 
     if(other.gameObject.tag == "Wall")
     {
         Vector3 vel = rigidbody.velocity;
         //Finding out the horizontal direction of the ball after hitting a wall
         Vector2 horizontalvector = new Vector2(vel.x, vel.z);
         //Normalizing the vector so only the direction remains without the speed
         horizontalvector = horizontalvector.normalized;
         //Adding the force of the original vector to the horizontal direction 
         horizontalvector = vel.magnitude * horizontalvector;
         //Now since the horizontal vector has the same force of the original velocity
         //All that remains is to give the ball the new velocity.
         //horizontalvector.x in x axis
         //horizontalvector.y in z axis
         rigidbody.velocity = new Vector3(horizontalvector.x, 0, horizontalvector.y);
     }
 }

So every time the ball hits something the force resulted from the collision will be directed to a horizontal direction with the same force.

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 Riskki · Aug 06, 2014 at 12:31 PM 0
Share

Thanks for your answers however it doesn't seem to work because when it hits the wall its almost like it uses the horizontal velocity and exchanges part of it to vertical velocity, so when you put the horizontal velocity back in after hitting the wall its much slower (and ofcorse this only sometimes happens so sometimes you get fast bounces other times slow bounces)

it acts as if the wall is a ramp however I've checked loads and its perfectly vertical!

what code do you wish to see?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make automatically moving sprite not passing through another sprite? 1 Answer

Prevent camera from going through walls and meshes 1 Answer

Adding Loft to a ball in a 3d golf game within Unity 2 Answers

Bouncing back 1 Answer

Sphere Goes Crazy 3 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