- Home /
Water splash issues...
Hi guys,
Because the default water prefabs don't have any scripts for splashes, I had to write this script:
#pragma strict
var splashPrefab : GameObject;
function OnTriggerEnter (other : Collider) { Instantiate(splashPrefab, other.attachedRigidbody.position, Quaternion.identity); }
This script was then attached to Water4Example (Advanced), and splashPrefab was set to Water Surface Splash (both prefabs are part of the standard assets). When something collides with my water, the splash appears and goes on forever. How do I time-limit it?
MachCUBED
Answer by hijinxbassist · May 29, 2012 at 06:47 AM
You can create a counter a couple of different ways. You can use a while loop or save the Time.time when the trigger happens and add count time to that, then have a conditional that destroys the object when the time is reached. While loop is better
#pragma strict
var splashPrefab : GameObject;
function OnTriggerEnter (other : Collider)
{
var sI=Instantiate(splashPrefab,other.attachedRigidbody.position,Quaternion.identity);
SplashCounter(sI.gameObject,3.6);
}
function SplashCounter(splashInstance:GameObject,timer:float)
{
t:float=0;
while(t<1)
{
t+=Time.deltaTime/timer;
}
Destroy(splashInstance);
}
Thanks hijinxbassist. $$anonymous$$ust say, though, that I wonder why the didn't implement this in their standard assets.
Answer by OZAV · Sep 27, 2014 at 04:00 PM
pragma strict
var t : float = 0 ;
var splashPrefab : GameObject;
function OnTriggerEnter (other : Collider)
{
var sI=Instantiate(splashPrefab,other.attachedRigidbody.position,Quaternion.identity);
SplashCounter(sI.gameObject,3.6);
}
function SplashCounter(splashInstance:GameObject,timer:float)
{
while(t<1)
{
t+=Time.deltaTime/timer ;
}
Destroy(splashInstance) ;
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Storing a part of a complex asset in an asset bundle 1 Answer
Switch the prefab of a game object from a script 2 Answers
Two exact objects behaving differently... *scratches head* 0 Answers
Script Communication between prefabs 1 Answer