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 /
  • Help Room /
avatar image
0
Question by Veerababu.g · Dec 16, 2015 at 11:00 AM · movementjumpjumping

how can make my player to jump like parabola

my character has to travel like this.whenever it lands on ground.

alt text

sign-wave.png (11.1 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

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by ZefanS · Dec 17, 2015 at 05:57 AM

Given that you are using a Rigidbody component on your player, thus utilizing the Unity physics engine, making an object move parabolically is fairly straight forward. Under only the influence of gravity, any projectile will travel in a parabolic arc. (See here for more info.) What you'll need to do is give the impulse when the player hits the ground in order to keep them traveling in this way.

In Unity, this can be achieved using the Rigidbody.AddForce() function. This function requires a vector pointing in the direction we wish the force to act in. That can be calculated according to the following diagram:

alt text

This is done in 2D for the sake of simplicity. The player's current position is given by (x,y). We want to impart a force at some angle θ to the ground. To do so, we need to calculate the vector leaving the player's position at that angle. Mathematically, this is the vector result of (x+a, y+b) - (x, y). We can solve for a and b using θ (assuming the hypotenuse of the triangle is of length 1), as such: a = cos(θ) and b = sin(θ).

Here is a simple implementation of this in Unityscript:

 #pragma strict
 
 public var theta : int;
 public var power : float;
 
 private var rb : Rigidbody;
 private var thetaRad : float;
 private var a : float;
 private var b : float;
 private var aim : Vector3;
 private var dir : Vector3;
 
 function Start()
 {
     rb = GetComponent.<Rigidbody>();
 }
 
 function OnCollisionEnter(collision : Collision)
 {
     if (collision.transform.tag == "Ground")
     {
         thetaRad = theta / 180.0f * Mathf.PI;
         a = Mathf.Cos(thetaRad);
         b = Mathf.Sin(thetaRad);
         aim = Vector3(transform.position.x + a, transform.position.y + b, transform.position.z);
         dir = aim - transform.position;
         rb.AddForce(dir * power);
     }
 }

This script should be attached to a GameObject with a Collider and Rigidbody. Note that the surface that the object will bounce on must be tagged "Ground" and have a Collider attached for this to work. Because the object retains its momentum through each bounce, the arc of the parabola will tend to widen the more the objects bounces. This can be counteracted by setting the Drag of the Rigidbody to some value above zero (for instance 0.1). By changing the values of theta and power you can adjust the shape of the parabola.

I thought of another way of doing this without using the physics engine but instead calculating the coordinates directly. If we want to calculate how the object will jump in a parabolic shape from one point to another, (provided that the points are at the same height) we can think of them as the roots of the parabola. Thus, given an initial (a,b) position and a distance we wish to jump, we know the other root will be (a',b), where a' = a + the jump distance. Thus we can figure out the equation that will give us all the coordinates in between.

That equation is : y = -x^2 + (a+a')x - aa' + b where b is the initial y value.

Then all we need to do is feed in the x-values and get the y-values, and we can move the object through the parabolic arc over a number of frames. The following script implements this method:

 #pragma strict
 
 public var range : float;
 public var numberOfFrames : int;
 private var x : float;
 private var xx : float;
 private var y : float;
 
 private var a : float;
 private var b : float;
 private var c : float;
 
 private var currentX : float;
 private var currentY : float;
 private var xDivision : float;
 
 function Start()
 {
     Parabola();
 }
 
 function Parabola()
 {
     x = transform.position.x;
     xx = transform.position.x + range;
     y = transform.position.y;
     
     a = -1.0f;
     b = x + xx;
     c = -x * xx;
     
     xDivision = range / numberOfFrames;
     
     for (var i = 0; i < numberOfFrames; i++)
     {
         currentX = x + i * xDivision;
         currentY = a * Mathf.Pow(currentX,2) + b * currentX + c + y;
         transform.position.x = currentX;
         transform.position.y = currentY;
         yield;
     }
 }

There are two public parameters: range and numberOfFrames. range determines how far the object will travel. numberOfFrames determines how many frames the object will take to move through the parabola. It can be thought as the inverse of the speed that the object will travel.

This method currently only works in 2D, but could be generalized to 3D by calculating the 2D trajectory coordinates and transforming them based on the direction the object should travel relative to the global coordinate axes.

Also note that this does not control for the maximum height of the object, and thus it will change according to the range.

Hope this helps.


diagram.png (32.6 kB)
Comment
Add comment · Show 2 · 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 Veerababu.g · Dec 17, 2015 at 09:54 AM 0
Share

the script is not working. i mean according to my requirement. .... i have fixed length of jumping...... and the player is traveling only left to right.

avatar image ZefanS Veerababu.g · Dec 18, 2015 at 01:13 AM 0
Share

Okay, well maybe if you modified it to meet your needs it would work. Also, note that you didn't state those requirements in the question.

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

33 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

Related Questions

Jump logic issues 0 Answers

Player looses ability to jump further right player moves. 1 Answer

My jump scripit wont work. i have been tryingtt o make it work for an eternity. please help. 1 Answer

Help with jumping script 2 Answers

Jump Using Raycast. 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