How you change another script value smoothly on trigger

Hello friendlies

I do following but it has two problem, first it happens on any collider and I want that only when player trigger it, and more the change is instant and I want it to be smooth over time, how fix it?

function OnTriggerEnter (other : Collider) {
var go = GameObject.FindWithTag("MainCamera");
go.GetComponent("SmoothFollow").distance = Mathf.SmoothStep(6, 20, 5);
}

Sorry for my English I use google translated

For your function OnTriggerEnter, the parameter “other” is what caused the trigger, so you can use that to check if it is the player. Something like:

function OnTriggerEnter (other : Collider) {
if(other.gameObject.name == "YourPlayerObjectName")
{
var go = GameObject.FindWithTag("MainCamera");
go.GetComponent("SmoothFollow").distance = Mathf.SmoothStep(6, 20, 5);
}
}

As for the instant change, I haven’t used SmoothStep before, but if its like other interpolating functions I think it needs to be in the Update function. You could try setting a boolean toggle to be true in the OnTriggerEnter function, then in Update, check if that boolean is true, and if so, do the SmoothStep, something like:

function OnTriggerEnter (other : Collider) {
if(other.gameObject.name == "YourPlayerObjectName")
{
changeDistance = true;
}
}

and in Update:

function Update()
{
...

if(changeDistance)
{
var go = GameObject.FindWithTag("MainCamera");
go.GetComponent("SmoothFollow").distance = Mathf.SmoothStep(6, 20, 5);
if(go.GetComponent("SmoothFollow").distance == 20)
{
changeDistance = false;
}
}

...
}

I don’t know if that will work perfectly, but hopefully its along the lines of what you are looking for. Good luck! :slight_smile:

It doesnt to work, but thank you :frowning:

I try use it to change smoothfollow.js it works for height smoothly but not distance that just pops instantly a change.

I think it is not possible. nothing works. a bug maybe

You can smooth it out by incrementing the smooth step in smaller increments each update cycle (I also haven’t used smooth step, but for other displacement code I do it that way). For example if you want the distance to move to be 20 and you want it to happen over 5 frames you would pass in the value of 4 for 5 updates rather than passing in the value of 20. I usually use Time.deltaTime * someModifier and that allows me to set the displacement exactly as I want it.