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 loupz · Feb 23, 2013 at 01:54 PM · velocitytrajectory

Help matching ball velocity to calculated trajectory?

Hello,

I'm trying to make a 2d game where the "jump" mechanic is similar to the Angry Birds catapult. I want to draw the trajectory on screen as the player pulls back the mouse and then fire the ball (player) along the path when the mouse button has been released.

I found a formula for plotting trajectory here.

I'm moving a square to debug the position of the trajectory and the first attempt seems to work. I move the mouse around the ball and the little square is opposite the mouse and the further I pull back the further from the player it goes. When I release the mouse button, the ball travels at the right direction, but not with enough force.

I'm not sure where I'm going wrong, could anyone shed some light on this? I've been stuck for a week or so now.

Thanks

 //waiting for mouse button to be lifted AND player initially clicked - FIRE!
 if (Input.GetMouseButtonUp(0) && clickedPlayer == true){
     playerCurrentPos.rigidbody.velocity = jumpVector * multiplier;
 }
 
 //Draw trajectory
 if (Input.GetMouseButton(0)){
     //Pull back position in screen space
     jumpEnd = Input.mousePosition;
     //Convert screen space to world space
     p = Camera.main.ScreenToWorldPoint (Vector3 (jumpEnd.x, jumpEnd.y, 12.7));
     //Try and detect angle
     angle1 = (p - playerCurrentPos.transform.position);        
     angle2 = new Vector3(1, 0, 0);
     angle = Vector3.Angle(angle1, angle2);
     //Getting opposite angle
     if (p.y <= playerCurrentPos.transform.position.y){
     jumpAngle = (180 - angle) * Mathf.Deg2Rad;
     }
     else{
     jumpAngle = angle * Mathf.Deg2Rad;
     }    
     //Draw vector (player position to mouse drag position)
     Debug.DrawLine(playerCurrentPos.transform.position, p, Color.red, 40);
     //Work out jump vector
     jumpVector = playerCurrentPos.transform.position - p;
     //Vetical and horizontal velocity calculation
     var xVelocity : float = (jumpVector.magnitude*multiplier) * Mathf.Cos(jumpAngle);
     var yVelocity : float = (jumpVector.magnitude*multiplier) * Mathf.Sin(jumpAngle);
     //Trajectory of X
     var trajX : float = xVelocity * 0.5;    
     //Trajectory of Y
     var trajY : float = (yVelocity * 0.5) - 0.5 * Physics.gravity.y * Mathf.Pow(0.5, 2);
     //Debug - position a square object in the scene to match the velocity of the projectile after 0.5 seconds
     square.transform.position = Vector3(trajX, trajY, 0);
 }


Comment
Add comment · Show 2
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 robertbu · Feb 24, 2013 at 02:49 AM 0
Share

You've posted a lot of code that would take a notable amount of time to figure out. I suggest you create a really cut down version of the problem...just a object launched at a specific angle along with the calculations on where it would land. You might find your own problem that way, or if you post it back here, it is more likely to get a response.

avatar image loupz · Feb 24, 2013 at 06:29 PM 0
Share

Thanks for the response robertbu, I'm new to scripting and not up on the etiquette of asking for help or showing off code. I've updated my original post and shortened the code to what I think is the crucial part of my problem. Any help on this would be much appreciated.

Thanks,

loupz

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Feb 24, 2013 at 07:02 PM

From http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

in order to get the height of an object at a distance x:

 y = yStart + x*tan(angle) - g*(x*x)/(2*((v*cos(angle))*(v*cos(angle))));

though there is probably a simplified version of this...

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 loupz · Mar 09, 2013 at 03:24 PM 0
Share

I managed to find some time to try this but it it seemed to just be a straight line and the X positions were fixed.

I've just given this a go:

     var Vxo = velocity * $$anonymous$$athf.Cos(jumpAngle);
     var Vyo = velocity * $$anonymous$$athf.Sin(jumpAngle);
     
     var trajX1 = playerCurrentPos.transform.position.x + Vxo * 1;
     
 var trajY1 = playerCurrentPos.transform.position.y + Vyo * 1 - 0.5 * -9.81 * Time.deltaTime * 1 * 1;
     
     square.transform.position = Vector3(trajX1, trajY1, 0);
     
     var trajX2 = playerCurrentPos.transform.position.x + Vxo * 2;
     
 var trajY2 = playerCurrentPos.transform.position.y + Vyo * 2 - 0.5 * -9.81 * Time.deltaTime * 2 * 2;        

     square2.transform.position = Vector3(trajX2, trajY2, 0);

I've still got the same issue as initially though. I am thinking it's to do with the way I'm calculating time as just a float or how I'm firing the ball:

 playerCurrentPos.rigidbody.AddForce(playerCurrentPos.transform.right * jumpVector.magnitude * multiplier, Force$$anonymous$$ode.VelocityChange);

I'm going to keep trying though.

Thanks

PS - This is where I've got my latest equation from:

http://www.convertalot.com/ballistic_trajectory_calculator.html

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving the player in a ballistic trajectory in 3D 0 Answers

Calculate a trajectory in all possible directions (2D). 2 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

How to calculate the velocity that needs to be applied to a flying object after hitting it in the air. 0 Answers

Trajectory for an angry birds like game 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