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 /
This question was closed Jul 07, 2020 at 10:54 PM by JMEJAY for the following reason:

Problem is not reproducible or outdated

avatar image
1
Question by JMEJAY · Feb 05, 2015 at 04:17 AM · movementmathfollowcurvesparabola

[Maths] How to project a curve in two axis? (Grav y + RelativeVelocity x)

Hello,

I am trying to make an object follow a curved line, and I am using the equation

  • y = ax^2 + bx + c

but I am also trying to affect this by a relative velocity value to give it extra curve in the x.

Currently I am inputting these values:

  • a = Gravity (9.81)

  • x = time

  • b = power (5)

  • c (start pos of object.y - 0)

  • d relativeVelocity

       void Update () {
    
         //relativeVelocity is found by mouseDelta.x
         DrawMyLine();
    
         
         }
     
             void DrawMyLine() {
         
         // y = ax^2 + bx + c + d
         // a = grav x = time b = power c = start pos d = relativeVelocity
         
         ballLine.SetVertexCount (rpg_exp);
         
         Vector3[] posArray = new Vector3[rpg_exp];
         
         float a; float b; float c; float d;
         float t = 0.0f; float dt = 0.2f;
         
         float gravity = Physics.gravity.y;
    
         float lineRelVel = relativeVelocity.x;
         
         
         for (int i = 0; i < rpg_exp; i++) 
         {
             
             a = (gravity/2) * (t*t);
             b = velocity.y * t;
             c = ballStart.y;
             
             
             posArray[i].y = a + b + c;
    
             b = velocity.x * t;
             c = ballStart.x;
             d = lineRelVel * t;  // I added this component, but it is wrong.
             
             posArray[i].x = b + c + d;
             lineRelVel += lineRelVel; // What am I doing...
             
             b = velocity.z * t;
             c = ballStart.z;
             
             posArray[i].z = b + c;
             
             ballLine.SetPosition(i,posArray[i]);
             
             t += dt;
    
             
             
         }
    
    
         
     }
    
    
    
    

alt text

Thanks for your time.

problem.png (148.4 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

4 Replies

  • Sort: 
avatar image
1

Answer by Orann · Feb 05, 2015 at 07:21 AM

It looks like you might want to be using Vector3's instead of flat floats for your equation there, like this. A Vector3 represents a position or velocity - in this case the vector for gravity is down so it looks like this: (0, -9.8, 0). It's good practice to use the built in references to things like gravity and time, so that your code is consistent with Unity's internal physics.

 Vector3 a = Physics.gravity;
 Vector3 b = new Vector3(10f, 60f, 0f);
 Vector3 c = new Vector3(0f, 0f, 0f) //start pos
 float x = Time.deltaTime; //This is how you generally get time in Unity - this will give you the time since the last frame
     
 Vector3 CurrentPos = a * (x * x) + b * x + c;

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
avatar image
1

Answer by fafase · Feb 05, 2015 at 05:02 AM

Considering you have gravity in your equation, I would think you are after projectile motion:

http://en.wikipedia.org/wiki/Projectile_motion

In this case, you use:

 y = originalForce * t * sin(angle) - 0.5 * gravity * (t * t);

the other way is to use AddForce with gravity enable, and let the engine do this computation.

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
avatar image
1

Answer by emc233 · Feb 05, 2015 at 05:10 AM

Translate does not do what you think. Search for transform.translate in this doc: http://docs.unity3d.com/ScriptReference/index.html

this code is in csharp:

 using UnityEngine;
 using System.Collections;
 
 public class phy : MonoBehaviour {
 
     private Vector3 initial_postition;
     private float start_time;
     float a;
     float b;
     float c;
     // Use this for initialization
     void Start () {
         start_time = Time.time;
         initial_postition = this.transform.position;
         a = 9.81f;
         b = 5;
         c = 0;
     }
     
     // Update is called once per frame
     void Update () {
     float x = Time.time - start_time;
     float y = a * x * x + b * x + c;
     this.transform.position = new Vector3(x, y, 0);
     }
 }
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
avatar image
1

Answer by moonstruck · Feb 05, 2015 at 09:18 AM

Hello! The equation you use is more of an accelerated straight movement where a is acceleration, b is initial velocity and c is starting position. If you want to move your object through parabolic trajectory then you can use the formula differently. First of all lets parametrize it using x = t. Thus we have y = a*t^2 + b*t + c. z should remain constant if you want the trajectory to be flat. Beware, a, b and c don't mean the same if you use the formula to define a trajectory, they define different forms of parabolas. The code can be rewritten:

 private float t = 0;
 
 void Update() {
    Vector3 newPos = new Vector3( t, a*t*t + b*t + c, 0 );
    transform.position = newPos;
    t = t + Time.deltaTime;
 }
 

You can experiment with different values of these parameters: a, b, c and initial t too, they can be negative like t = -1.

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 JMEJAY · Feb 10, 2015 at 03:29 AM 0
Share

Thanks for this. I wonder if you can help me once more, in that my parabola needs to take into account the objects relativeVelocity on the x axis. I can't quite figure it out. I imagine it is just another component to the equation.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

move object with parabola 0 Answers

Making a bubble level (not a game but work tool) 1 Answer

Adding curve to Vector3.MoveTowards 1 Answer

How to Follow By Getting KeyDown? 0 Answers

Moving forward towards the cursor with LookAtMouse? 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