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
5
Question by Karsnen_2 · Sep 20, 2012 at 05:28 PM · c#iosinputforce

[SOLVED] Derive an equation from a graph?

I am trying to figure out a formula to apply as my mechanic. My user gives in One Input and I should apply it as two outputs. i.e. two output forces

I have sketched a graph depicting how I need the forces to be. But I am not able to figure out a way to derive an equation to find it out.

The input is basically a tilt and the amount of tilt is calculated to apply force.

Can you guys help me to derive a equation for this?

THE GRAPH

alt text

Same image is also here, just in case.

Thank you,

Karsnen.

forumla.jpg (173.4 kB)
Comment
Add comment · Show 7
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 Screenhog · Sep 20, 2012 at 05:43 PM 0
Share

Just to check... are there any curves involved, or are both forces entirely linear?

avatar image AlucardJay · Sep 20, 2012 at 05:47 PM 1
Share

I think for this kind of thing, you may be better suited using Animation Curves than an equation. Then it becomes flexible which would be handy for tweaking values associated with force.

avatar image Karsnen_2 · Sep 20, 2012 at 05:51 PM 0
Share

Screenhog : They are not linear. They are more like to be a curve.

Alucardj : I am sorry, I am not able to get you.

avatar image AlucardJay · Sep 20, 2012 at 05:54 PM 0
Share

Basically it is a window like the animation window, you add nodes and form curves, then ask to evaluate at position X, and it returns value at position Y. I think it may be better if I make an answer with a script to demonstrate. Here's the API links :

http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.html

http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.Evaluate.html

avatar image Karsnen_2 · Sep 20, 2012 at 05:56 PM 0
Share

So can I use them as a catalyst between my input and the output? I thought they serve only for particle effects, sounds and animation of meshes.

Show more comments

1 Reply

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

Answer by AlucardJay · Sep 20, 2012 at 06:06 PM

Attach this script to any gameObject. Note in the inspector 2 rectangular windows with the assigned var names. Click on a window. You are now in the curve editor. Select one of the preset curves at the bottom, then add / remove / move / rotate nodes to form curves.

I personally like to keep my values 'normalized' i.e. on the positive axis between 0 and 1. But as long as you know what range you are accessing and keep that in mind when forming your curves, there shouldn't be a problem. Infact from memory I once called a value x that had no curve that high, and it returned 0, but definitely no error. Have a play around. If you read x as time % 1 and affect a transform of a cube, you'll have a visual representation of how the curve is read. ut basically for a 2D graph it is give me Y for value X

 #pragma strict
 
 public var curveForceA : AnimationCurve;
 public var curveForceB : AnimationCurve;
 
 public var valueX : float;
 
 function Update() 
 {
     if ( Input.GetMouseButtonUp(0) )
     {
         var curveA : float = curveForceA.Evaluate( valueX );
         Debug.Log( " Force A returned " + curveA );
         
         var curveB : float = curveForceB.Evaluate( valueX );
         Debug.Log( " Force B returned " + curveB );
     }
 }

I took a while typing the description =]

http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.html

http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.Evaluate.html


I have written an example of a curve reader, this can be done without playing the scene. YOu need to change the script, typecast the variables to the names of your scripts, and also name the curves the same as the var names in the script to be copied.

So for my example, the script to be copied is CurvesExample, it is the above script.

I have an empty gameObject with a script called CurvesBackup1, this is all that is on it :

 #pragma strict
 
 public var curveForceA : AnimationCurve;
 public var curveForceB : AnimationCurve;
 
 function Start() {    
 }
 function Update() {    
 }

And finally an empty gameobject with the script reader. Drag and drop the example object and the backup object in the inspector, then right-click on the inspector and select "Read Curves"

The curve window don't update straight away, you have to click off the object and then back on. Check the backup object has the curves copied to it, including the changes in tangent handles.

To write a curve to a script, just reverse-engineer the below to read from backup, and write to the script using. Here is the read script :

 #pragma strict
 
 #if UNITY_EDITOR
 
 public var scriptExample : CurvesExample;
 public var scriptBackup : CurvesBackup1;
 
 public var curveForceA : AnimationCurve;
 public var curveForceB : AnimationCurve;
 
 private var ks : Keyframe[]; 
 
 @ContextMenu ("Read Curves")
 function ReadCurves() 
 {
     Debug.Log("Reading Curves from ContextMenu");
     
     
     // curveForceA
     ks = new Keyframe[ scriptExample.curveForceA.length ]; 
     
     for ( var i:int = 0; i < scriptExample.curveForceA.length; i ++ )
     {
         ks[i] = Keyframe(     scriptExample.curveForceA[i].time, 
                             scriptExample.curveForceA[i].value, 
                             scriptExample.curveForceA[i].inTangent, 
                             scriptExample.curveForceA[i].outTangent     );
     }
     
     curveForceA = new AnimationCurve( ks ); // Read and Store Curves from scriptExample
     scriptBackup.curveForceA = new AnimationCurve( ks ); // Write Curves to scriptBackup
     
     
     // curveForceB
     ks = new Keyframe[ scriptExample.curveForceB.length ]; 
     
     for ( i = 0; i < scriptExample.curveForceB.length; i ++ )
     {
         ks[i] = Keyframe(     scriptExample.curveForceB[i].time, 
                             scriptExample.curveForceB[i].value, 
                             scriptExample.curveForceB[i].inTangent, 
                             scriptExample.curveForceB[i].outTangent     );
     }
     
     curveForceB = new AnimationCurve( ks ); // Read and Store Curves from scriptExample
     scriptBackup.curveForceB = new AnimationCurve( ks ); // Write Curves to scriptBackup
     
 }
 
 #endif

to use @ContextMenu, right-click where it says Curves Reader (Script) in bold :

alt text


readcurves.png (24.6 kB)
Comment
Add comment · Show 10 · 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 AlucardJay · Sep 20, 2012 at 06:08 PM 1
Share

For the transform, add this at the start :

 var theCube : Transform;

and in the function :

 theCube.position.x = curveForceA.Evaluate( Time.time % 1.0 );
avatar image Karsnen_2 · Sep 20, 2012 at 06:09 PM 2
Share

$$anonymous$$ $$anonymous$$ay aka alucardj - thank you. I would trying it out.

avatar image Fattie · Sep 20, 2012 at 08:30 PM 2
Share

fantastic example introducing "the curve thingy" to the world, awesome

avatar image Karsnen_2 · Sep 21, 2012 at 05:02 PM 1
Share

It just works like a beauty.. Just did even think about this. Thanks $$anonymous$$ $$anonymous$$ay.

I have one more concern. I am not able to save the curve. I declare the variable and then alter the curves inside the inspector pane. Then if I

  1. Use the same script on a different gameobject (or)

  2. Change the variable names

I lose the curve. I predicted this happening but is there a way to save them?

avatar image Karsnen_2 · Sep 21, 2012 at 08:07 PM 1
Share

$$anonymous$$ $$anonymous$$ay - hmm Yeah sure.. I am going to work on that whenever I could. If I happen to get one I will let you know for sure. But for the moment, the temp solution I take is to record all (x,y) key point values and a screen shot of the curve. This helps a bit not comprehensive.

Show more comments

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How good is my "Quaternion" code efficiency? 1 Answer

How to successfully apply force calling a function from another script 1 Answer

Multiple Cars not working 1 Answer

Why is Input.GetAxisRaw() not returning whole numbers when using a joystick? 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