Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LucasMars · Aug 09, 2013 at 02:09 PM · variablemouse button

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LucasMars · Aug 09, 2013 at 03:00 PM 0
Share

Thanks for your answer. Your code seems to slow down the process of destroying.

avatar image mactinite77 · Aug 09, 2013 at 03:05 PM 0
Share

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.

avatar image LucasMars · Aug 09, 2013 at 03:07 PM 0
Share

Thanks for the extra comment! It was too fast (actually!) and I fixed that. Thanks!

avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LucasMars · Aug 09, 2013 at 02:42 PM 0
Share

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

avatar image meat5000 ♦ · Aug 09, 2013 at 02:48 PM 0
Share

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.

avatar image LucasMars · Aug 09, 2013 at 02:55 PM 0
Share

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]

avatar image meat5000 ♦ · Aug 09, 2013 at 03:05 PM 0
Share

Thanks :))

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges