Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SirMacJefferson · Sep 09, 2011 at 05:29 AM · functionframesmoothdamp

Experiencing strange problems with Mathf.SmoothDamp function

So, here's the code I"m using with Mathf.SmoothDamp:

 currentAccuracy = Mathf.SmoothDamp(currentAccuracy,Accuracy,vel,RecoveryTime);

"vel" is a float that's set to 0.0

Basically, it's just setting your current accuracy to your accuracy over RecoveryTime. Or it's supposed to do that.

RecoveryTime grows larger every time you fire, up to a maximum of 1 (so 1 second). I know that works because it stays under or equal to 1 in the inspector.

For some reason, that line of code only affects currentAccuracy if currentAccuracy is under 0. Then, it sets currentAccuracy up to 0 over RecoveryTime (I think it's over RecoveryTime...but either way, it's still behaving unexpectedly for me). Why isn't it setting currentAccuracy to Accuracy, and why is it only affecting currentAccuracy if currentAccuracy is under 0?

When I set SmoothTime to 1 instead of RecoveryTime, then it seems to work without requiring currentAccuracy to be under 0, but it doesn't set it to Accuracy..it sets it slightly under it for some reason.

Now, when I was fiddling around with it, it seems to start spiking my FPS to 10 on every second frame and then letting it back up to 80 the next frame...

Can anyone tell me what I'm doing wrong?

Thanks!

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 Owen-Reynolds · Sep 10, 2011 at 03:32 PM

Are you setting vel to 0 just once (or each time you fire,) then leaving it alone?

SmoothDamp uses vel as its memory of the previous speed. Each call, SmoothDamp adjusts vel, then , next call, uses vel as the "old speed." If, say, it's 0 each call, SmoothDamp never gets up to any speed. For fun, if you make vel public, you should see SmoothDamp changing it.

Comment
Add comment · Show 8 · 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 SirMacJefferson · Sep 14, 2011 at 05:57 AM 0
Share

I was setting it just once at the very start of the script...I tried each time I fired, but it still reacted the same. In the Inspector, vel stays at 0 throughout the entire thing...

Am I supposed to be doing something different with vel?

avatar image Owen-Reynolds · Sep 14, 2011 at 11:21 PM 0
Share

Retried it -- vel should be changing. If it somehow doesn't, then currectAcc should still be changing very slowly. Being slightly under the final value is normal -- it's supposed to slow down before the target. Umm...javascript...is anything accidentally an int?

avatar image SirMacJefferson · Sep 16, 2011 at 05:19 AM 0
Share

currentAccuracy was an integer and so was Accuracy, so I changed them to floats and it started to work, but vel wasn't changing and it took like, 10 seconds ins$$anonymous$$d of RecoveryTime (which is always anywhere from 0.0-1.0 via $$anonymous$$athf.$$anonymous$$in). I tried giving each weapon its own vel and using that, but it said it didn't come up with a definition for $$anonymous$$athf.SmoothDamp(System.Object,System.Object,System.Object,System.Object) Something like that. I guess for some reason, vel doesn't count as a float anymore - it counts as an object...? Because I put it in the class that holds each weapon's individual stats and referenced it to that. I don't really know what to do now...it seems like this function is a whole lot of work. $$anonymous$$aybe I should try making my own or something, heh...

avatar image Owen-Reynolds · Sep 16, 2011 at 06:30 AM 0
Share

If vel never changes, smoothDamp always thinks you're going from a complete stop, so it should take forever. The entire problem is that vel is always 0. For a test, try setting vel to 1, every frame. If it still says 0, that really narrows it down.

You didn't already have a vel for each weapon? I can't think of how it would be any way besides having SmoothDamp and vel in the weapon script.

avatar image SirMacJefferson · Sep 17, 2011 at 07:17 AM 0
Share

Right now, I use a For loop to go through each weapon once, calling $$anonymous$$athf.SmoothDamp on each one with the For's "i" value as the array number (you have 5 weapons). However, when I try to replace vel with (weapon class).vel, it says the error I mentioned above. Why is that? Is it not considered a float because it's part of the class..?

Show more comments
avatar image
0

Answer by Zach Stefanakis · Mar 22, 2012 at 05:54 PM

So simple yet so hard to discover... Thank you very much! the above works.

Best change vel to zero continuesly if you are using smoothdamp

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Call a function dependent on frame of animation? 2 Answers

How do I tell another script to skip its routine for one frame? 1 Answer

How do you find Unity's frames/second? 4 Answers

Ensure that function only gets called once in a frame 2 Answers

How to change GUI.DrawTexture with OnMouseEnter function? 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