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 boddole · Feb 20, 2014 at 11:07 PM · c#mathformula

Trouble with Basic Math Formula for Deriving Velocity

Hello everyone, I'm having trouble understanding how a value (in the script below) is being derived. In the game scene, I have this script attached to a column that moves up and down, and I can confirm that currentPosition and previousPosition are different values. However, the values of x, y , and zVel are always 0. I don't understand what is wrong with the formula where 2 different numbers can equal 0.

     public Vector3 currentPosition;
     public Vector3 previousPosition;
 
     public float xVel;
     public float yVel;
     public float zVel;

     void Update ()
     {
         currentPosition = transform.localPosition;
         //Debug.Log ("currentPosition is " + currentPosition);
         Debug.Log ("current Y position is " + currentPosition.y);
 
         xVel = currentPosition.x - previousPosition.x;
         yVel = currentPosition.y - previousPosition.y;
         Debug.Log ("yVel is " + yVel);
         zVel = currentPosition.z - previousPosition.z;
 
         MovePosition ();
 
         previousPosition = transform.localPosition;
         //Debug.Log ("previousPosition is " + previousPosition);
         Debug.Log ("previous Y position is " + previousPosition.y);
 
 
     }

     void MovePosition ()
     {    
         if (nextPhase == false && inMotion == false)
         {
             transform.localPosition = Vector3.MoveTowards (transform.localPosition, endingPosition, movementSpeed * Time.deltaTime);
 
             if (Vector3.Distance(endingPosition, transform.localPosition) < 0.01f)
             {
                 inMotion = true;
                 if (inMotion == true) //&& Time.time > waitToRetract)
                 {
                     StartCoroutine (WaitForRetract(rectractTimer));
                 }
             }
         }
         
         if (nextPhase == true && inMotion == false)
         {
             transform.localPosition = Vector3.MoveTowards (transform.localPosition, startingPosition, movementSpeed * Time.deltaTime);
             if (Vector3.Distance(startingPosition, transform.localPosition) < 0.01f)
             {
                 inMotion = true;
                 if (inMotion == true)
                 {
                     StartCoroutine (WaitForRestart(restartTimer));
                 }
             }
         }
     }
Comment
Add comment · Show 7
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 rutter · Feb 20, 2014 at 11:17 PM 1
Share

Can we see the variable declarations? If you're using integers to store floats, the resulting values will be truncated.

avatar image boddole · Feb 20, 2014 at 11:27 PM 0
Share

@rutter: sure, I have added them to the question.

avatar image mattyman174 · Feb 20, 2014 at 11:32 PM 0
Share

Can you provide the code to your $$anonymous$$ovePosition() $$anonymous$$ethod as well please.

avatar image boddole · Feb 20, 2014 at 11:38 PM 0
Share

@mattyman174: sure, adding that to the question as well.

avatar image Serdnad · Feb 21, 2014 at 12:32 AM 0
Share

Just saying, in the code you've posted, inmotion will always be true on from that first time you set it to true.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RudyTheDev · Feb 21, 2014 at 12:57 AM

This is backwards:

 MovePosition ();     
 previousPosition = transform.localPosition;

You can't move first, then remember the "previous" position. You have to remember where you were, then do movement. Otherwise you remember the new position, which will be the currentPosition next frame (unless something extra happens after Update(), but then you will only account for that extra change).

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 boddole · Feb 21, 2014 at 01:29 AM 0
Share

@$$anonymous$$TheDev: I think the problem is where I'm doing the float calculations. When I think about it I'm basically saying:

 Awake()
 {
 store whereIAm
 store whereIWillBe
 //which will be the same, then:
 }
 
 Update ()
 {
 store whereIAm (say its 0)
 
 Do calculations
 
 $$anonymous$$ove ()
 
 store whereIWillBe (say its 1)
 }

The problem is, when Update is called again, whereIAm will equal 1, while whereIWillBe is also still 1 - which makes sense. What doesn't make sense (to me anyway), is how I'm getting different values for those 2 values since they should be the same.

avatar image boddole · Feb 21, 2014 at 02:11 AM 0
Share

$$anonymous$$oving where the calculation was done seems to have solved the problem, it doesn't fully explain what was going on, but I'll take what I can get.

avatar image RudyTheDev · Feb 21, 2014 at 10:39 AM 0
Share

You could be getting different values because some other movement happens in between Update() calls, like some rigid body physics in FixedUpdate().

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

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

Related Questions

Programmatically specifying a random point in an arc in front of the player 1 Answer

Finding the axis of a curving pipe 0 Answers

A fast triangle triangle intersection algorithm for unity? 4 Answers

Shortest distance from player to ball direction 2 Answers

Multiple Cars not working 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