So hopefully this is an easy one.
I have a series of objects tagged “Pods-Leader” all running on along a path animation that i have brought in from an fbx file from 3dsmax.
The problem i am having is getting the animation state to stop and start basted on the raycast distance. It works when i am setting the distance for it to slow and speed up. But i would like it to set the speed back to 1.0 if a ray hasn’t hit after say 3 seconds.
This is the js code i am using.
#pragma strict
function Update () {
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit, 15.0 ))
{
if(hit.collider.gameObject.tag == "Pods-Leader")
{
GetComponent.<Animation>()["PodMovement"].speed = 0.75;
}
if (Physics.Raycast(transform.position, transform.forward, hit, 7.5 ))
{
if(hit.collider.gameObject.tag == "Pods-Leader")
{
GetComponent.<Animation>()["PodMovement"].speed = 0.5;
}
if (Physics.Raycast(transform.position, transform.forward, hit, 5.0 ))
{
if(hit.collider.gameObject.tag == "Pods-Leader")
{
GetComponent.<Animation>()["PodMovement"].speed = 0.0;
}
Debug.DrawLine (transform.position, hit.collider.transform.position,Color.red);
}
}
}
}
Any help would be gratefully received.
Thanks.
You can use a timer to check the last time it was hit and then set the timer based on that timer value. Something like:
#pragma strict
var lastHitTime = 0;
function Update ()
{
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit, 15.0 ))
{
if(hit.collider.gameObject.tag == "Pods-Leader")
{
GetComponent.<Animation>()["PodMovement"].speed = 0.75;
}
if (Physics.Raycast(transform.position, transform.forward, hit, 7.5 ))
{
if(hit.collider.gameObject.tag == "Pods-Leader")
{
GetComponent.<Animation>()["PodMovement"].speed = 0.5;
}
if (Physics.Raycast(transform.position, transform.forward, hit, 5.0 ))
{
if(hit.collider.gameObject.tag == "Pods-Leader")
{
GetComponent.<Animation>()["PodMovement"].speed = 0.0;
}
Debug.DrawLine (transform.position, hit.collider.transform.position,Color.red);
}
}
lastHitTime = 0.0;
} else
{
// Since there was no hit add time to the lastHitTime
lastHitTime += Time.deltaTime;
if(lastHitTime > 3.0) // This means your last hit time was before 3 seconds
{
GetComponent.<Animation>()["PodMovement"].speed = 1.0;
}
}
}