- Home /
Durablility / Destroy Cube if mouse is down
I have some code which I want to destroy a cube if a variable is 0f. What I want is if the player is holding the left-mouse button, then I want the variable (that starts at 25f) to Lerp to 0f as you are holding the mouse-button down and when it isn't being pressed, I want it to Lerp back to 25f. Here is my code:
var durability : float;
var object : GameObject;
var smooth : float;
function Start () {
durability = 25f;
}
function OnMouseOver() {
if(Input.GetMouseButton(0)){
Debug.Log("Input Down");
durability = Mathf.Lerp(durability, 0f, Time.time);
yield WaitForSeconds(1);
}
}
function OnMouseUp() {
durability = Mathf.Lerp(durability, 25f, Time.time);
}
function Update() {
if(durability<=0f){
Destroy(object);
}
}
But this instantly destroys the cube. Where am I going wrong?
Answers are appreciated.
Answer by mactinite77 · Aug 09, 2013 at 02:38 PM
The documentation says :
Interpolates between a and b by t. t is clamped between 0 and 1.
When t = 0 returns from. When t = 1 return to. When t = 0.5 returns the average of a and b.
and for Time.time it says this :
The time this frame has started (Read Only). This is the time in seconds since the start of the game.
So if you are 30 seconds into the game, the value for t or time is equal to 1 returning your to variable which in this case is 0.
To make this bit of code work we need to grab the time from when we started lerping, this is up to you how you grab that as there are many different ways, but I use a boolean check and a GetTime function usually. It all depends on how you want your code to look.
now you can also create your own time parameter and reset that, fopr example:
var t : float = 0;
t += 1 * Time.deltaTime;
just reset that t to zero right before you lerp.
Hope this helps!
Thanks for your answer. Your code seems to slow down the process of destroying.
No problem! If you're using the t += 1 * Time.deltaTime; and it seems too slow, just change the 1 to a bigger number or a variable you can change.
Thanks for the extra comment! It was too fast (actually!) and I fixed that. Thanks!
Answer by meat5000 · Aug 09, 2013 at 02:36 PM
I Believe Time.time is the time passed since the game began, which would mean that aside from the first 1s the time is over 1.
Lerp t factor must be between 0 and 1 (just like animation weights) and so you need to make it vary between 0 and 1 over time. If the function has only 1 chance to be called i.e it is inside a function that is not called every frame the Lerp will fail as it returns a scaled value between the two input factors each call that must be handled. It will not perform the whole Lerp for you.
Check out my lengthy entry in this thread : http://answers.unity3d.com/questions/229690/issues-with-lerps.html see if that helps.
Here is my code:
var durability : float;
var object : GameObject;
var smooth : float;
var spawnLerp = 10f;
var lerpTimer;
function Start () {
durability = 25f;
}
function On$$anonymous$$ouseOver() {
if(Input.Get$$anonymous$$ouseButton(0)){
Debug.Log("Input Down");
durability = $$anonymous$$athf.Slerp(durability, 0f, lerpTimer/spawnLerp);
lerpTimer += Time.fixedDeltaTime;
}
}
function On$$anonymous$$ouseUp() {
lerpTimer = 0;
durability = $$anonymous$$athf.Slerp(durability, 25f, lerpTimer/spawnLerp); lerpTimer += Time.fixedDeltaTime;
}
function Update() {
if(durability<=0f){
Destroy(object);
}
}
But It gives me an error:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) BlockDestroyWait.On$$anonymous$$ouseOver () (at Assets/Scripts/BlockDestroyWait.js:14) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)
What is wrong with the code?
Thanks
I don't think $$anonymous$$athf.Slerp is a function. $$anonymous$$athf.Lerp is ok. You can have Vector3.Slerp and Quaternion.Slerp. Note that Slerp is Spherical Lerp. Try declaring spawnLerp like this :
var spawnLerp : float = 10;
Also On$$anonymous$$ouseUp() is a momentary event so the lerp would only be called once. Remember one call of lerp doesnt perform the whole movement. You need to keep tending to it, changing the value of t. The lerp then returns the value at some point between from and to where from = 0 and to = 1.
Thanks, I still cannot get the code to work. Here is my code now:
[EDIT] I used mactinite77 answer in the end, but your answer has also been voted [/EDIT]
Your answer
Follow this Question
Related Questions
Communication between objects and other scripts, variables and properties 1 Answer
Rewind/Play animations on demand. 1 Answer
Can I show a numeric variable from the slider on the GUI? 1 Answer
variable inside prefab 1 Answer
How to: make changes to a var over real time units instead of frame units? Mathf.Lerp 1 Answer