• 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 t$$anonymous$$s?

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
avatar image AlucardJay · Sep 20, 2012 at 05:47 PM 1
Share
avatar image Karsnen_2 · Sep 20, 2012 at 05:51 PM 0
Share
avatar image AlucardJay · Sep 20, 2012 at 05:54 PM 0
Share
avatar image Karsnen_2 · Sep 20, 2012 at 05:56 PM 0
Share
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 t$$anonymous$$s 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 $$anonymous$$gh, 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 w$$anonymous$$le 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, t$$anonymous$$s 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, t$$anonymous$$s 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
avatar image Karsnen_2 · Sep 20, 2012 at 06:09 PM 2
Share
avatar image Fattie · Sep 20, 2012 at 08:30 PM 2
Share
avatar image Karsnen_2 · Sep 21, 2012 at 05:02 PM 1
Share
avatar image Karsnen_2 · Sep 21, 2012 at 08:07 PM 1
Share
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Touch Input with Event Trigger. How to handle with it properly. 0 Answers

Inputing an Equation 0 Answers

Multiple Cars not working 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