Hi, i wonder how to add some “channeling” function into the game?
Let’s say i have a cube and a sphere. If the cube collides with the sphere, the sphere will transform to a capsule. Now I know how to do this but i want something more advanced…
the cube MUST collide with the sphere for x seconds (let’s say 3 seconds) before the sphere transform to capsule. If let’s say I move back the cube (so it doesnt collide anymore) before the 3 seconds channeling time finish, the sphere must not transform to the capsule. So basically it will only transform to capsule IF the sphere collides with the cube for 3 seconds.
OnCollisionStay is what you need. Just have it add to your “timer” every time it is called (once per frame) and OnCollisionExit to reset it if the cube leaves before the set time.
Edit;
Their are also trigger version if you need them
Edit edit;
OnCollisionStay can be pretty heavy if collision info is needed for every frame. So a while loop that is called by OnCollisionEnter and ended by OnCollisionExit would be better.
well not sure what’s the difference between OnCollisionEnter and OnTriggerEnter (other : Collider) but apparently it works too so im using the OnTriggerEnter one…
this is what i have atm:
now everytime the cube collide with the sphere, it changes (triggerYeah) to a capsule but when I move back the cube I get the print “success”.
How to add the timer like you said to work with the OnTriggerEnter code above? Add the if function below other.gameObject.tag? and what should be in the if function? I only know yield.WaitForSeconds…
Also how to cancel the whole transformation process in OnTriggerExit?
var timerOn : boolean = false;
var curTime : float = 0.0;
var timeNeeded : float = 3.0;
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "Player") StartTimer();
}
function StartTimer(){
while(timerOn == true){
if(curTime >= timeNeeded){
//do whatever you need to do to change the sphere here
timerOn = false;
curTime = 0.0;
}
else curTime += Time.deltaTime;
yield;
}
}
function OnTriggerExit(other : Collider){
if(other.gameObject.tag == "Player"){
timerOn = false;
curTime = 0.0;
}
}
Should be all you need. Hand typed though so it might have errors.
The diffrence between OnCollision*** and OnTrigger***. Is that OnCollision*** is called when the gameObject’s collider is hit. And OnTrigger*** is when the gameObject’s trigger “hit”.