- Home /
Can you gradually slow an fbx animation on raycast hit, then speed it up again after a certain amount of time?
Hi all,
Not sure how well i have explained this, but hopefully someone out there will understand what i want to do..
I have a series of path animations baked into an fbx file that i would like to gradually slow down using a raycast that shoots a ray forward, checks the tag on the gameobject then slows the animation state speed if the tag on the gameobject is marked "pod". I have some javascript working for this so far, but i would like it to gradually slow the animation whereas at the moment it slows it instantly. Once it has slowed for a period of time, lets say 5 seconds, i would then like it to speed up again until the next raycast hit is called.
This is the code i have so far:
#pragma strict
function Update () {
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit, 10.0 ))
{
if(hit.collider.tag == "Pods")
{
for (var state : AnimationState in GetComponent.<Animation>())
state.speed = 0.05;
print (hit.collider.tag);
Debug.DrawLine (transform.position, hit.collider.transform.position,Color.red);
}
}
}
Also i have noticed that the frame rate drops dramatically when there are a lot of raycast hits. I think this casts a ray every frame? Should/could i cast the ray in a different way to get less of an impact on frame rate? Or am i tackling this in completely the wrong way?
Any help would be hugely appreciated!
Many thanks,
Paul
Removing the print and debug has drastically improved the frame rate, so thanks for that idea. If i could understand how to gradually slow and speed up the animation this might work for me now. I'm also trying a different approach using an OnTriggerEnter and OnTriggerExit approach without any raycasting. Problem is still the gradual slowing and speeding up of the animation though.
You can change the speed of the Animation Layer with Animator.
http://docs.unity3d.com/ScriptReference/Animator-speed.html
I don't think you can change each Animation individually, but only the whole layer.
thanks for the pointer towards the Animator component..However I still dont get how i access it to slow the animation speed in the script.
Animator wont work for you if you have done you animation in Legacy 'Animation' component.
Otherwise, using Animator, you can simply reference the animator in script, and set its speed.
Answer by meat5000 · Jan 19, 2015 at 10:51 AM
Raycast is expensive. GetComponent is expensive. 'for this in that' is expensive. Debug is expensive.
Yes. This is probably the wrong approach, but try removing the print and Debug and see if that is the reason for bad performance.