• 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 baris150490 · Dec 28, 2013 at 11:11 AM · mathfformulamagic

Is there a Magic Code for getting delta values?

Hey, i am wondering that if there is any magic code or function that gets the delta of any floating value between last and current frame? Like a Mathf but doing a compare job between frames.What would be the ways of doing it?Any magic formula for this?I know that Time.deltaTime is irrelevant depending on the fps and gets always the same value.So could we do a math over it? If we multiply a floating value with Time.deltaTime would it give a kind of delta for us?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by KellyThomas · Dec 28, 2013 at 11:15 AM

No magic formula, just manual tracking:

 float lastFrameValue;

 void Update() {
     float deltaValue = currentFrameValue - lastFrameValue;
     //other stuff
     lastFrameValue = currentFrameValue;
 }  

Wash, rinse, repeat.

You could build a system that took an ID and current value pair, then returned the delta but it's not part of the unity API and it would be slower to execute than using the pattern above.

Comment
Add comment · Show 7 · 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 baris150490 · Dec 28, 2013 at 11:24 AM 0
Share

I am using the code above as well but i am not satisfied with it is there any alternative to that please?

avatar image baris150490 · Dec 28, 2013 at 11:46 AM 0
Share

here is why i am not satisfied with it..

alt text

untitled.png (106.9 kB)
avatar image baris150490 · Dec 28, 2013 at 11:47 AM 0
Share

here is also the code;

 #pragma strict
 var target : Transform;
 var oldAngle : float;
 var newAngle : float;
 var angle : float;
 oldAngle = $$anonymous$$athf.Atan2((target.position - transform.position).x, (target.position - transform.position).z) * $$anonymous$$athf.Rad2Deg;
 
 function Update ()
 {
      if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
      {
      target.RotateAround(Vector3.zero, Vector3.up, 5);
      }
      else
      {
      target.RotateAround(Vector3.zero, Vector3.up, -5);
      }
 newAngle = $$anonymous$$athf.Atan2((target.position - transform.position).x, (target.position - transform.position).z) * $$anonymous$$athf.Rad2Deg;
 angle = newAngle - oldAngle;
 oldAngle = newAngle;
 Debug.Log(angle);
 }

As well as angle won't increase correctly, it does increase to 270 then all in sudden change to -90, i don't know why.

avatar image KellyThomas · Dec 28, 2013 at 12:15 PM 1
Share

Ok then, the problem isn't tracking deltas between floats. The problem is dealing with a "wrapped" number space i.e. 360 = 0, 359 = -1.

avatar image KellyThomas · Dec 28, 2013 at 05:13 PM 0
Share

In your code above, please try replacing Debug.Log(angle); with:

 Debug.Log("old: " + oldAngle + ", new: " + newAngle + ",delta: " + angle);

You will see why the numbers behave like they do.

One useful trick is to normalize them to the range -180 to 180.

This ensures that we have the smallest angle between the other two angles.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image

Related Questions

mathf.sin((Mathf.PI / 180) * 180) get 0.8966.. 2 Answers

Distance in a Trajectory of a projectile 0 Answers

managing floating point rounding 1 Answer

Unity for coders 2 Answers

calculus help angels of a triangle 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges