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

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.