Sunday, June 26, 2022
HomeGame Developmentunity - transfer an object when a particular occasion happens?

unity – transfer an object when a particular occasion happens?


I’m not a fan of coroutines, as a result of they make it laborious to purpose what truly goes on within the code. I want to make use of a sample like this:

public class MoveOnTrigger : MonoBehaviour {
     
     personal bool isGoing;
     personal float elapsedTime;
     public float desiredTime;

     void Replace() {
           if (isGoing) {
               elapsedTime += Time.deltaTime;
               float percentageComplete = elapsedTime / desiredTime;
               remodel.place = Vector3.Lerp(startPos, MoveHere, Mathf.SmoothStep(0,1, percentageComplete));

               if (percentageComplete >= 1.0f) {
                   isGoing = false;
               }
           }

     }

     public void LetsGo() {
          elapsedTime = 0f;
          isGoing = true;
     }
}

The gist is that you simply do your motion in Replace, however have a bool variable (isGoing on this case) which prevents that code from working. When the triggering occasion occurs, you set that variable to true, and the motion begins.

Additionally notice that this code lets you repeat the motion from the start by calling LetsGo() once more. That is achieved by resetting elapsedTime again to 0.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments