Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Lerping outside of an Update Function

$
0
0
I have, for a long time now, tried using mathf.lerp outside of an update function but as of yet have never figured out how. The value simply jumps or goes half way. In the case of my current script, I'm trying to lerp the size of an object when it spawns. My problem is that the lerp is called before the object is instantiated into the game (which seems impossible to me as the script isn't even in the game yet).

    function Update(){
    if(Spawner.Starter == 1) {
    Debug.Log('worked' + Spawner.Starter);
    //Spawner.Starter is just a number I tried to use to tell this script when to start, it   didn't 
    //work though
    transform.localScale = new Vector3.(Mathf.Lerp(2, 0.1, Time.time/3), Mathf.Lerp(2, 0.1, Time.time/3), 0);
    }
    }

the sphere this code is assigned to is supposed to get smaller. Going from 2.0 x and y scale to 0.1 scale x and y. It should be called whenever the object is instantiated (keep in mind the object isn't even in the game anywhere until is is instantiated so I have no idea what is causing the script to run on awake even when the objects aren't active or in there) I don't know if the code for the instantiating process is relevant but here it is; var ThickWall : GameObject; var ThinWall : GameObject; var SpawnLocale : Transform; static var Starter : int = 0; yield WaitForSeconds(1); Spawn(1, 0); yield WaitForSeconds(1); Spawn(1, 0); function Spawn(num : int, rotation : float) { if(num == 1) { var clone : GameObject; clone = Instantiate(ThickWall, SpawnLocale.position, Quaternion.Euler(0, 0, rotation)); clone.transform.localScale = Vector3(2, 2, 0); Spawner.Starter = 1; Spawner.Starter = 0; yield WaitForSeconds(3); Destroy(clone); } if(num == 2) { var clone1 : GameObject; clone1 = Instantiate(ThinWall, SpawnLocale.position, Quaternion.Euler(0, 0, rotation)); clone1.transform.localScale = Vector3(2, 2, 0); Spawner.Starter = 1; Spawner.Starter = 0; yield WaitForSeconds(3); Destroy(clone1); } } I've been going over this problem for 6 hours now and have yet to find any solutions online or elsewhere. When the object is instantiated in (I instantiate it one second after the game runs) it starts out at a scale (it's a 2d circle so I will only be referring to a single plane size) of 1.5, even though the objects original scale is 2.0. It continues on with the lerp after this. The value is interpolating properly but it is starting to do it when it isn't supposed to. I'm making a game like super hexagon where it clones walls that spawn in on you (they shrink). Now when the first wall spawns in, it follows the lerp from where the lerp currently is (the lerp is working before I spawn the object in). When I spawn the second object, it spawns at the same position of the first object. It's as if the lerp function is starting when I start the game (which would be impossible since the script isn't even in the game yet) and not when the object with the script is instantiated. The interpolation happens once when the game starts and lasts 3 seconds. Any individual objects with the script that spawn in will keep their scale at the value of interpolation as if it is a global variable effecting everything. I really need help because to me this problem seems impossible. If the update function is removed the object simply spawns in and never shrinks or scales at all.

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>