Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by F00bzor · Oct 03, 2012 at 01:46 PM · curveanimationcurvecurves

AnimationCurve, how can i know when the end of the curve has been reached?

In my little test project, i decided that I want to lern how to use animationcurve. There is not much documentation but i managed to have an object move using a curve i created. Unfortunately, the object continue to move into infinity since i dont know how to tell it to stop.

The problem is, i have no idea how to specify a lengh of time for my curve and tell my script to stop running when it reached it. Strangely, curve.length tells me the number of key, which is not what i want.

Let's say i want to animate a light.range, with a min intensity and a max intensity, over a specified time, using a curve how would i do that in C#?

I searched a lot and i couldn't find an answer that can be apply to my situation.

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

3 Replies

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

Answer by AlucardJay · Oct 03, 2012 at 02:25 PM

suggested steps :

find the time of the last keyframe

check if foobar > last keyframe . time

if so then stop, else evaluate

Here's how to find the time and value for the last keyframe :

 public var myCurve : AnimationCurve;
 var lastKeyTime : float;
 
 function FindLastKeyframe()
 {
     // CHECK if there is NO CURVE
     if ( myCurve.length == 0 )
     {
         Debug.Log( "NO CURVE ..." );
         return;
     }
     
     var lastframe : Keyframe = myCurve[ myCurve.length - 1 ];
     
     lastKeyTime = lastframe.time;
     
     Debug.Log( " lastframe.time = " + lastframe.time );
     Debug.Log( " lastframe.value = " + lastframe.value );
 }
 
Comment
Add comment · Show 6 · 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 F00bzor · Oct 03, 2012 at 02:42 PM 0
Share

Thank you i could get the time and value of the lastframe, but i am getting IndexOutOfRangeException with that script. How can it be fixed ?

avatar image AlucardJay · Oct 03, 2012 at 02:48 PM 0
Share

Did you actually put any keyframes (or a preset curve) in myCurve? This is just a bare example, so I havn't included a check for if there is no curve or keyframes. This script is tested and working (it works even with just one keyframe), make sure you actually have a curve in the window =]

  • have edited my answer to check if there is no curve or keyframes present*

avatar image F00bzor · Oct 03, 2012 at 06:27 PM 0
Share

it still won't work as intended :( I have created a curve in the inspector and applied it to my prefab so unless that the problem, yes my projectile got a curve.

Now i am realizing that the first instance of the object actually runs the curve just fine(but still wont destroy itself) But every instance of this object is at the same value as the first spawned one regardless of when it spawned. ie: it takes 2 second to reach maximum light.range, if a projectile spawn after that, light.range are set to maxrange.

$$anonymous$$y code is messy now since i have over 200 code line of tests commented here and there, anyone can make a quick code for it so i better understand how it should be done ? A light, that, on spawn, change its light.range over 2 second and then destroy itself.

avatar image AlucardJay · Oct 03, 2012 at 06:38 PM 0
Share

I just scrolled up to check your code, didn't see any but realized that you are coding in C#. Sorry about that, but it seems you are more than capable of converting it (there are no compile errors so that is not the issue). Have you tried just running a C# version of my answer, add a key up input check in Update to run the method, then modify the curve and see if at least the last key is being read and it is returning the time of the last key?

$$anonymous$$y last guess is to check the prefab in the project window has the curve, all instantiated instances of that prefab should inherit the curve, I shall test further to investigate (having not instantiated an object with a curve myself). In the meantime, I think I understand your request and shall look into writing a script that can do that.

While I am happy to, you should also consider the design of this object. Do you know how long it lives for? If yes, then simply call a destroy with a delay on start, then make sure your curve is profiled so that the intensity falls to or below zero before the delay set in the destroy. For alot of instantiated objects, this calculate definite time periods for events could save valuable CPU loading =]

Also I just noticed you said you are learning the curves, maybe some info on my answer here may help with that : http://answers.unity3d.com/questions/320689/derive-an-equation-from-a-graph.html

avatar image F00bzor · Oct 03, 2012 at 06:44 PM 0
Share

Thank you for your answers ! No problem for converting Js to C# i can manage :)

I just cleaned everything and your code does work, ,it still gives me OutOfRange Error but it is working... might be me converting to C# that forgot some C# rule. It was my other problem that lead me to thing it didnt work, (every instance of this object are set to the same time) i created a new thread for this one in case no solution can be found here. http://answers.unity3d.com/questions/326912/projectiles-with-animated-light-are-all-animating.html

awaiting your script :)

Show more comments
avatar image
0

Answer by Zerot · Oct 03, 2012 at 02:03 PM

You need to use http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.Index_operator.html in combination with length. You get the last key by getting the one at index "length-1" and then you check its time field and compare it to your internal time field.

So, `if (curve[curve.length-1].time>t) Stop()`

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 F00bzor · Oct 03, 2012 at 02:23 PM 0
Share

Thank you for the answer, i am messing around with the index, but i still cannot get it to do something when it reaches the end.. Could you post a "working" piece of code ?

I am also getting out of bound error

avatar image
0

Answer by Hi_ImTemmy · Oct 21, 2021 at 07:36 PM

I just hit this very issue today.

My solution in C# which I believe is the same as the steps outlined:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CurveQues : MonoBehaviour
 {
     public AnimationCurve curve;
 
     private float curveTime;
     private float lastKeyTime;
 
     void Start()
     {
         FindLastKey();
     }
 
     void FindLastKey()
     {
         //Get the last keyframe and log it's time
         Keyframe lastKey = curve[curve.length - 1];
         lastKeyTime = lastKey.time;
     }
 
     void Update()
     {
         curveTime += Time.deltaTime;
 
         if (curveTime < lastKeyTime)
         {
             // Do stuff like curve evaluation
         }
         else
         {
             // End of animation curve!
         }
     }
 }

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

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

12 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

Related Questions

How can I set the tangents of Keyframes in an AnimationCurve through scripting? 1 Answer

Create a linear animation curve with javascript 1 Answer

Why does Undo.RecordObject not work after EditorGUILayout.CurveField? 1 Answer

Animation Events and Mecanim 2 Answers

Tranform Animation Curve ruins Gravity 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