Search Unity

Creating a trend, and assigning point value to a variable :S

Discussion in 'Scripting' started by MatLmco, Apr 16, 2015.

  1. MatLmco

    MatLmco

    Joined:
    Apr 16, 2015
    Posts:
    6
    I may have bit off more than I can chew, (newbie) and any help to guide me in the right direction would be greatly appreciated.

    1- I need to set var A and var B to specific values, based on var C. (easy enough...but)
    2- As var C increases, the value of var A decreases based on a trend line
    3- As var C increases, the value of var B increases based on a trend line

    So my question is, how do you create trends to get values from, and then assign them?

    I have 4 plot values for each of the variables, and var C will be manually controlled.

    I guess an example would be: as my RPM increases (var C), down-force would decrease (var A), but turbo PSI would increase (var B).
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  3. MatLmco

    MatLmco

    Joined:
    Apr 16, 2015
    Posts:
    6
    OK thanks, I will look into how to use that. I came across it, but was worried it was not quite what I should use. Thanks again!
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It's weird to use, you have to pass in your independent variables as time. But the curve editor is powerful and indispensable for this type of work.
     
  5. mdcmouse1

    mdcmouse1

    Joined:
    Dec 23, 2012
    Posts:
    5
    Yeah I can see that! I think it is going to take me some time to figure this out ;-)
     
  6. mdcmouse1

    mdcmouse1

    Joined:
    Dec 23, 2012
    Posts:
    5
    Can the AnimationCurve still be used if the 3 variables are not used in an animation?
     
  7. mdcmouse1

    mdcmouse1

    Joined:
    Dec 23, 2012
    Posts:
    5
    OK, I think I got part of it, my 3 variables are there with the 4 data points I have for each variable. Just need to work through this, figure out how to pull my values off it now...
     
  8. mdcmouse1

    mdcmouse1

    Joined:
    Dec 23, 2012
    Posts:
    5
    I am down to 2 variables, and using time as my third. I can't for the life of me figure out how to access the animation to get the values of my 2 variables based on the keyframe I specify. Any help would be greatly appreciated...
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Forget about using an animation, you are simply trying to use the AnimationCurve. Here is some demo code to illustrate the idea. Assumes your curve goes from 0 to 1.

    Code (CSharp):
    1. public class TestCurve  : MonoBehaviour {
    2.     [SerialiseFeild] AnimationCurve theCurve;
    3.  
    4.     void Start (){
    5.         for (int i = -; i < 10; i++){
    6.             float x = theCurve.Evaluate(i/10);
    7.             Debug.Log(i.ToString() + " : " + x.ToString());
    8.         }
    9.     }
    10. }
     
  10. MatLmco

    MatLmco

    Joined:
    Apr 16, 2015
    Posts:
    6
    Thanks again for your help! I came across this example yesterday, but not sure how to use it for what I need. (plus I am in Jscript).

    I have a pretty big curve since I am plotting points with RPM as my scale (700 - 2500). As the RPM gets higher, my GPM gets lower, but PSI gets higher. these do not increase/decrease in a linear way, and I have very specific numbers for each at a given RPM.

    I thought I had it figured out by using the time scale as my RPM (0-1800), and I plotted my GPM and PSI points which gave me 2 good curves to start with (until I can get additional plot points for each). My idea was to take my RPM variable, and set the keyframe to this value giving me the GPM and PSI for that RPM. Just couldn't figure out how to set the keyframe and access the values.

    You say there is a better way to do this (and I have come across many examples like the one above), I am just having a real hard time wrapping my head around how to use it lol. I fear this is going to take me quite some time to figure out!
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This is pretty much exactly what I'm suggesting you do. Access the variables with AnimationCurve.Evaluate (RPM). You can set key frames in the inspector by double clicking on the curve.
     
    MatLmco likes this.
  12. MatLmco

    MatLmco

    Joined:
    Apr 16, 2015
    Posts:
    6
    OK cool, so I was following :) I think maybe I was just burnt, had been coding all weekend, and when I got on that my brain was not co-operating lol

    I tried to get the AnimationCurve.Evaluate to work, but kept getting errors. I was not coding it right (still learning). I am going to keep trying to get it. Thanks for the direction, at least I know I am pointed in the right direction!
     
  13. MatLmco

    MatLmco

    Joined:
    Apr 16, 2015
    Posts:
    6
    Me again! I have been at this all week, and I still haven't gotten it. I have been trying so may things but nothing is working. Any chance you could throw a dog a bone? I attached my script as well as my animation. I think I need to assign my animation to my " var curve : AnimationCurve;", but have no idea how...
     

    Attached Files:

  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I still think we are on different pages. Here is the simplest example I could build. Hit play. Put a number in the input field between 1 and 0 and watch the text fields update themselves according to the animation curves. You can set the animation curves in the inspector.

    You don't have to use 0 to 1, I just made the curves that way.
     

    Attached Files:

    MatLmco likes this.
  15. MatLmco

    MatLmco

    Joined:
    Apr 16, 2015
    Posts:
    6
    Thank you so much for your help. This whole time I have been using curves I made as an animation and trying to get it assigned to the curve in the Inspector lol I should be able to finally move forward this weekend, will let you know how it goes! :-D Thanks again!
     
    Kiwasi likes this.