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 Daniloleemes · Oct 09, 2014 at 05:17 AM · collisionphysicsrigidbody

Raycast on ball game

Hi everyone! I'm having some problems with physics on my project. When a ball collides with a collider or other ball in a considerable speed, both balls get the inverse direction (thats right). But, if one ball is too slow, the faster one, hits and takes the inverse direction, but the one which was slow, continues slowing down due to friccion on ground. I want to at every collision the balls get some speed boost in the inverse direction. I think you understand... (my english is sooooooooo bad :( ) Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class ballControl : MonoBehaviour {
     
     Rigidbody myRigidbody;
     Vector3 oldVel;
 
     void Start () {
         myRigidbody = GetComponent<Rigidbody>();
     }
     
     void FixedUpdate() {
         oldVel = myRigidbody.velocity;
     }
 
      void OnCollisionEnter (Collision c) {
 
 
         ContactPoint cp = c.contacts[0];
         // calculate with addition of normal vector
          myRigidbody.velocity = oldVel + cp.normal*1.0f*oldVel.magnitude;
         
         // calculate with Vector3.Reflect
         myRigidbody.velocity = Vector3.Reflect(oldVel,cp.normal);
         
         // bumper effect to speed up ball
         myRigidbody.velocity += cp.normal*1.0f;
 
     } 
 }
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

2 Replies

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

Answer by Habitablaba · Oct 09, 2014 at 09:00 PM

   // calculate with addition of normal vector
   myRigidbody.velocity = oldVel + cp.normal*1.0f*oldVel.magnitude;

You don't need to multiply by 1.0f here, that does nothing.
Do you mean to be multiplying by oldVel.magnitude here? Since you're not normalizing oldVel, it still contains the direction and magnitude data. This means you're essentially squaring it. This would be a big reason one object is shooting off and the other is not.

If you are looking for them to both shoot off at the same speed, you'll need to do some maths to transfer momentum from one object to the other.

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 Daniloleemes · Oct 09, 2014 at 09:10 PM 0
Share

Wasn't exaclty what im looking for, but worked. Thanks again. I have one last problem, wanna help? <3

avatar image
0

Answer by dmg0600 · Oct 09, 2014 at 03:59 PM

Where is the raycast part of your problem? Please construct your question according to your problem.

If the ball is stopped or moving really slowly at the time of the impact it most certainly won't react as its rigidbody will be asleep and won't recieve any callback.

If you want an asleeped rigidbody to react to OnCollisionEnter you can wake it up every frame although it might be costly in performance. Use rigidbody.WakeUp() for this.

You can read about sleeping rigidbodies in the documentation.

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

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

Camera gets flung off of the map when the player collides with certain objects. 0 Answers

Nav Mesh Agent won't fall off the platform? 1 Answer

Stop rigidbodies from bouncing 3 Answers

Tossing an object with the correct force at any distance from another so that it collides with the other 1 Answer

Collision not working properly in a Fixed Joint 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