Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 hozthemage · Jul 01, 2017 at 06:43 PM · transformpositionvector3math

Creating a bouncing game without using physics - Vector3 math problem,

So I apologize if I over-explain this.

I'm creating a bouncing without using gravity or physics materials. Right now there are five possible spots on the x-axis where a ball is able to be (on the x-axis the spaces are -2.5, -1.25, 0, 1.25, 2.5) the ball bounces up in the y-axis and is unaffected by z.

After an initial bounce, the ball is able to go to any of the 5 spaces outlined on the x-axis. The balls will travel in a curve and land on one of the 4 other possible spaces. It's also set up to go down on the y-axis to -5 (the paddles that intercept the ball are laid out on -3 on the y-axis) so the objective of the game is to move the paddle to one of 5 spots to intercept the ball. For an example of this is, when a ball hits a paddle in one of the spots, it will travel from (-2.5, -3, 0) to (1.25, -5, 0)

However, because I have the balls going all the way down to -5 on the y-axis the spots where the paddle intercepts the ball are not in the middle of one of the points on the x-axis I outlined earlier (-2.5, -1.25 etc.), They end up being either short or over-extended where they should be because they need to travel the extra 2 to reach -5.

Setting the y value on the bounced position from -5 to -3 or so fixes the problem but then the animation ends and the ball doesn't descend further. The solution I'm trying to figure out is instead of bouncing it on the x-axis on one of the bounce positions I outlined earlier (-2.5, 1.25 etc.), instead of aiming for one of the x-axis values directly, plot an imaginary point at -5 where the paddle space would intercept the ball. However, I don't have the foggiest on the math I would need to calculate the x value of that imaginary point.

This is the code I'm using to plot the point currently

 endPos = new Vector3 ((paddleSpaces [nextSpace].GetComponent<Transform> ().position.x), -5f, 0f);

Where paddleSpaces is an array of GameObjects containing the five spaces, nextSpace is a random number between 0 and 4.

And this is what I'm using to calculate the curve animation

         while(Time.time < timeStamp + (bounceTime/gc.speedCo)){
 
             Vector3 currentPos = Vector3.Lerp(startPos, endPos, (Time.time - timeStamp)/(bounceTime/gc.speedCo));
 
             currentPos.x += bending.x * Mathf.Sin (Mathf.Clamp01 ((Time.time - timeStamp) / (bounceTime/gc.speedCo)) * Mathf.PI);
             currentPos.y += bending.y * Mathf.Sin (Mathf.Clamp01 ((Time.time - timeStamp) / (bounceTime/gc.speedCo)) * Mathf.PI);
             currentPos.z += bending.z * Mathf.Sin (Mathf.Clamp01 ((Time.time - timeStamp) / (bounceTime/gc.speedCo)) * Mathf.PI);
 
             transform.position = currentPos;
 
             if (bounced) {
                 yield break;
             }
 
             yield return null;
         }

Please, any help that you guys can offer me in this would be greatly appreciated. I confess I've been stuck on this problem for a couple days and my different attempts to solve it have not gotten me very far.

Thank you. ,

Comment
Add comment · Show 3
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 Major · Jul 01, 2017 at 08:03 PM 0
Share

Uh... I have to admit I do not really know what you are trying to do. BUT, there are a few things that I know with fair certainty you will need when it comes to doing your own physics with falling things and bouncing:

 Vector3.Reflect
 
 y = vy * t + 0.5*a*t^2 <-- this is used with the vertical component of your velocity vector
 x = vx*t <-- this is your horizontal displacement 
 
 y = vertical displacement
 t = time
 vy = initial velocity (vertical component of velocity vector)
 a = gravity
 
 x = horizontal displacement
 vx = horizontal velocity (horizontal component of velocity vector)


The two equations will give you the next position of the ball, and you can use them to predict any point that you want. Vector3.Reflect is important for bouncing things off other things. These equations and function will give you a physically accurate model.

But I really don't have much of an idea beyond that as to what you need.

Vector3.Reflect

avatar image aldonaletto · Jul 01, 2017 at 11:58 PM 0
Share

It's hard to figure out what you're talking about. I suggest that you post a small video showing what's happening.

avatar image hozthemage · Jul 02, 2017 at 03:53 PM 0
Share

NP, I'm adding a reply that might help explain it.

2 Replies

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

Answer by Bampf · Jul 02, 2017 at 05:03 PM

Instead of Lerping to the end position and stopping, allow the ball to pass through endPos and keep going. The loop would need to stop when y is less than -5 (or when there is a paddle bounce, but it looks like you handle that case already.)

To make the loop work this way you'd need to change a few things.

1) Replace while condition

 while (Time.time < timeStamp + (bounceTime/gc.speedCo)){ 

with something like

 while (transform.position.y >= -5.0f) {

2) Replace Lerp with LerpUnclamped, to allow t to go past 1.

3) I'm pretty sure you will need to remove the calls to Mathf.Clamp01. I don't know why they are needed, and will probably prevent the curve from being smooth.

4) (optional) The code will be more readable if you calculate (Time.time - timeStamp)/(bounceTime/gc.speedCo) once and store it in a variable, e.g.

 float scaledElapsedTime = (Time.time - timeStamp)/(bounceTime/gc.speedCo);
 Vector3 currentPos = Vector3.Lerp(startPos, endPos, scaledElapsedTime);
 // etc

You will probably need other changes as well, but hopefully you understand the idea.

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 hozthemage · Jul 02, 2017 at 06:01 PM 0
Share

That worked like a charm! Thanks!

avatar image
0

Answer by hozthemage · Jul 02, 2017 at 04:02 PM

I added a visual representation of what I'm trying to accomplish. So the five spaces are being outlined as the white spaces (currently the paddle is resting on (-1.25, -3). The user is able to move to any of these spaces by touch. The paddle has successfully bounced the ball and now the ball is headed towards space (1.25, -3), this is selected at random, it could be any of the spaces.

alt text

The ball must go down to -5 (to be considered a loss) and it's the player's job to intercept the ball at the paddle spot to keep the game in play before that. Once the ball gets to -5 on the y-axis, it's considered a miss and the game is over.

So I know that on the path of this curve the ball will make, it must pass through (1.25, -3) to land smack dab in the middle of the paddle space for the paddle to intercept it. What I'm trying to figure out is what the x value of the final ball destination must be to accomplish that. As I said before the ball must get down to -5, but what's the x-value going to be?


curve-math.jpg (25.9 kB)
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

91 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

Related Questions

How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers

Trouble setting up a vector 3. 1 Answer

Problem with local position 3 Answers

Distance won't work 1 Answer

Any help with prevent camera from passing a specific coordinate? 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