• 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 OneCode-Official · May 07, 2020 at 03:32 PM · 2dphysicsvelocitymathportal

How does the velocity of a ball flying throw a portal change when the portal is rotated?

So i tried a lot of stuff but nothing worked. I hope someone of you is better in physics then me. I think the picture represent my problem pretty good but if you need any details let me know. alt text

portal.png (11.0 kB)
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
2
Best Answer

Answer by darksider2000 · May 09, 2020 at 01:36 PM

Hi @OneCode-Official

The complexity of the code to do this depends on a number of things:

  1. Will your ball enter the portal at an angle? Or will it always enter straight?

  2. Is your ball also affected by gravity or other external forces when it enters?

  3. Does your ball have any angular velocity?

  4. Will rotation need to be taken into account?

  5. Is there any change to speed when going through the portal?


I'll assume the answers to 3, 4 and 5 are all no.

If your ball always exits straight all you need is

 Vector3 currentVelocity = rigidbody.velocity; // rigidbody = ball.GetComponent<Rigidbody>();
 Vector3 newVelocity = exitPortal.transform.forward * currentVelocity.magnitude;

If you want your ball to exit at the same relative angle with which it entered then

 Vector3 currentVelocity = rigidbody.velocity;
     Vector3 enterVelocity = enterPortal.transform.InverseTransformDirection(currentVelocity);
     Vector3 exitVelocity = exitPortal.TransformDirection(enterVelocity);



 // This takes the world space ball's velocity vector, and gives a velocity vector that is local to the enter portal. This vector can then be applied at the exit portal to get the same relative direction as in entry.

It's important to note that magnitude is unaffected by InverseTransformDirection().


I haven't tried the code myself but it should work in principle. Let me know if it doesn't work or if there are more variables to consider in finding a solution.

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 OneCode-Official · May 09, 2020 at 06:36 PM 0
Share

This worked out for me but I had to take the exitVelocity * -1. Thank you very much!

avatar image
0

Answer by iveL_newO · May 07, 2020 at 05:18 PM

From your question's tags, it looks like your game is 2d, which makes this so much easier. What you need to do is find a vector that can represent the difference of your velocity and the rotation of your portal. To do this you need to divide your entrance portal's .foreward by your ball's velocity. You then need to multiply your exit portals .foreward by the new vector.

 Vector3 difference = enterPortal.transform.foreward / ball.rigidbody.velocity;
 ball.transform.position = exitPortal.transform.position;
 ball.rigidbody.velocity = exitPortal.transform.foreward * difference;

Do note that you can't actually get ball.rigidbody, you'll need to reference the rigidbody2d in some other variable. If this doesn't work (which it very well might not, I'm not incredibly confident in my vector maths) you might want to try saving the magnitude of your velocity. This would make the code look like this:

 Vector3 difference = enterPortal.transform.foreward / ball.rigidbody.velocity;
 float magnitude = ball.rigidbody.velocity.magnitude;
 ball.transform.position = exitPortal.transform.position;
 Vector3 newVelocity = exitPortal.transform.foreward * difference.normalized * magnitude;
 ball.rigidbody.velocity = newVelocity;


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 OneCode-Official · May 09, 2020 at 12:33 PM 0
Share

Thanks for your answer. I tried it out and modified it a lot but it doesn't seem to work out no matter what I am trying. The velocity changes to (0,0) every time.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Physics in 2d arkanoid. Tangential and normal velocity 1 Answer

Velocity Movement & Physics Interactions by Rigidbody2D 0 Answers

How do I get velocity from a collision? 1 Answer

How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

Inconsistent Rigidbody2D velocity. 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