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 delta_angelfire · Jul 17, 2017 at 04:41 AM · rotationpositioningdeltatime

Beginner question about using deltaTime

So I've got Game manager code

 public static string GamePhase = "Planning";
 public static float MovementTimer = 2.0f;
 private static float MovementCountdown = MovementTimer;

 // Use this for initialization
 void Start () {


 }
 
 // Update is called once per frame
 void Update () {

     if(Input.GetButtonDown("Submit") && GamePhase == "Planning")
     {
         MovementCountdown = MovementTimer;
         GamePhase = "Movement";
         Debug.Log ("Phase is now" + GamePhase);
     }
 
     if(GamePhase == "Movement")
     {
         MovementCountdown -= Time.deltaTime;
         if (MovementCountdown <= 0.0f) 
         {
             GamePhase = "Planning";
             Debug.Log ("Phase is now" + GamePhase);
         }
     
     }
         
 }

and unit code

 public float Heading = 90.0f;
 public float Speed = 1.0f;
 void Start () {
     
 }
 
 // Update is called once per frame
 void LateUpdate () {
     if (GamePhaseManager.GamePhase == "Movement") 
     {
         var x = Time.deltaTime * Heading / GamePhaseManager.MovementTimer;
         var z = Time.deltaTime * Speed / GamePhaseManager.MovementTimer;
         transform.Rotate(0, x, 0);
         transform.Translate(0, 0, z);
         Debug.Log ("Ship is moving!");
             
     }
 }

}

In theory this should make my ship travel a distance of 1 length while turning 90 degrees over a time period of 2 seconds each time I hit the submit button. However, every time it executes, the angle ends up off by ~.5 degrees, and if I try to do a full circle (4 executions) I end up at position x = 0.1019182, position z =-0.1206948, and rotation y = -1.814. What do I need to change so that the movement ends up more precise (exactly 90 degrees every turn)?

Comment
Add comment · Show 1
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 Jwizard93 · Jul 17, 2017 at 04:55 AM 0
Share

I think you may want to switch which scripts use Update and LateUpdate. $$anonymous$$aybe you are missing that last frame of movement. $$anonymous$$aybe not though I'm not sure.

These seems like an ideal place for lerp though. Do you know how to use that function?

And given the phase structure of your game you could end the phase by correcting for small errors.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HarshadK · Jul 17, 2017 at 05:58 AM

It's because your gameobject is not rotating on the last frame of the rotation cycle because you are rotating in LateUpdate and your Update method from GamePhaseManager sets the phase to planning before your LateUpdate gets a chance to rotate the last increment.

Consider your game is running at 60 FPS then the approx deltaTime is 0.0166 (1/60). So your each rotation value of x as per your line of code:

 var x = Time.deltaTime * Heading / GamePhaseManager.MovementTimer;

will be:

 x = 0.0166 * (90/2) = 0.747 (~0.5)

But since deltaTime is not exactly the same as we have assumed (0.0166) you are getting an approx rotation delta lag of 0.5.

You can change your LateUpdate to Update and since you want to have your unit code run after GamePhaseManager you can set the script execution order for these two scripts. Or rather than doing that you can apply correction at the end when rotation ends to have your object rotate to exact value.

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

88 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 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 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 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

How to position objects UI elements in a line accounting for the rotation of the object 1 Answer

Rotating an object so that the direction between it's children matches the forward of rotating object 2 Answers

Frame Independent Movement with AddRelativeForce 1 Answer

I am having a weird issue with Quaternion.FromToRotation 2 Answers

Projectile not launching from correct place - rotation or positioning issue? 0 Answers


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