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 DoubleIsLoveDoubleIsLife · Jan 11, 2015 at 06:07 PM · timelerpframeratetime.deltatime

Time.deltaTime not working correctly?

*EDIT: Working code:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class WeaponSway : MonoBehaviour {
 
     public float moveAmount = 50f;
     public float moveSpeed = 2f;
     public float maxMoveAmount = 100f;
 
     public GameObject gun;
     public GameObject firePoint;
 
     private float moveOnX;
     private float moveOnY;
 
     private Vector3 defaultPOS;
     private Vector3 newPOS;
 
     public bool aiming = false;
     public Vector3 hipPOS;
     public Vector3 aimPOS;
 
     void Start() {
         defaultPOS = transform.localPosition;
     }
 
     void Update() {
         if (Input.GetKeyDown (KeyCode.Mouse1)) {
             if (firePoint.GetComponent<TestGun>().aiming) {
                 firePoint.GetComponent<TestGun>().aiming = false;
             }
             else {
                 firePoint.GetComponent<TestGun>().aiming = true;
             }
         }
         if (firePoint.GetComponent<TestGun>().aiming) {
             defaultPOS = aimPOS;
         }
         else {
             defaultPOS = hipPOS;
         }
         moveOnX = Input.GetAxis("Mouse X") * moveAmount;
         moveOnY = Input.GetAxis("Mouse Y") * moveAmount * 1.5f;
 
         if (moveOnX > maxMoveAmount) {
             moveOnX = maxMoveAmount;
         }
         if (moveOnX < maxMoveAmount *-1) {
             moveOnX = maxMoveAmount *-1;
         }
         if (moveOnY > maxMoveAmount) {
             moveOnY = maxMoveAmount;
         }
         if (moveOnY < maxMoveAmount *-1) {
             moveOnY = maxMoveAmount *-1;
         }
         RaycastHit hit;
 
         newPOS = new Vector3 (defaultPOS.x+moveOnX, defaultPOS.y+moveOnY, defaultPOS.z);
         Debug.DrawRay (transform.position, transform.forward, Color.blue);
 
         if (Physics.Raycast(transform.position, transform.forward, out hit, 1.5f)) {
             newPOS.z+=(Vector3.Distance(transform.position, hit.point)*-1);
         }
 
         gun.transform.localPosition = Vector3.Lerp(gun.transform.localPosition, newPOS, moveSpeed*Time.deltaTime);
     }
 }
Comment
Add comment · Show 5
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 Klarax · Jan 12, 2015 at 02:59 PM 0
Share

use FixedUpdate - and Time.fixedDeltaTime to eli$$anonymous$$ate the frame rate issue

using these will always result in 1 being 1 second

avatar image meat5000 ♦ · Jan 12, 2015 at 03:03 PM 0
Share

At a glance, I'd say the problem is in the Lerp. The time factor remains the same but you are moving the goal-posts.

Pick predefined constant values for your Lerp rather than continually working it out based on its current position.

Alternatively work it in as an Additive animation.

avatar image NoseKills · Jan 12, 2015 at 06:37 PM 0
Share

I think @meat5000 is on the right track. Everybody and their cousin is using Lerp "wrong" every day on Answers :) It's very common to just stick deltaTime as the 3rd parameter without thinking what it actually means.

If you Lerp(currentPos,target,Time.deltaTime), with framerate of 1 or less, you travel 100% of the distance in 1 second. With frame rate of 2 you travel 50% of the way during the first 0.5sec, and 50% of the remaining distance during the next 0.5sec, amounting to a total of 75% of the way in 1 second... and so on. So the greater the frame rate, the lesser the movement. Exactly what you are experiencing.

avatar image Eric5h5 · Jan 12, 2015 at 08:21 PM 0
Share

@$$anonymous$$larax: do not use FixedUpdate. It's only for physics, and will slow down if the framerate is too low, depending on your settings. @DoubleIsLoveDoubleIsLife: Time.deltaTime is simply the time delta since the last frame and is completely impossible for it to ever "not work correctly".

avatar image InvincibleCat · Jan 12, 2015 at 08:28 PM 0
Share

@Eric5h5 not totally agree with the fixed update thing. FixedUpdate should be used in this case for the movement Update part thought

1 Reply

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

Answer by InvincibleCat · Jan 12, 2015 at 07:04 PM

On each conditions like

 if (moveOnX > maxMoveAmount * Time.deltaTime) {
              moveOnX = maxMoveAmount * Time.deltaTime;
          }

just remove Time.deltaTime and it should work.

so it will be

 if (moveOnX > maxMoveAmount) {
              moveOnX = maxMoveAmount;
          }
          if (moveOnX < -maxMoveAmount) {
              moveOnX = -maxMoveAmount;
          }
          if (moveOnY > maxMoveAmount) {
              moveOnY = maxMoveAmount;
          }
          if (moveOnY < -maxMoveAmount) {
              moveOnY = -maxMoveAmount;
          }

But in my opinion, don't clamp your value... That makes no sense...

Cheers

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 NoseKills · Jan 12, 2015 at 07:34 PM 0
Share

The deltaTime in Lerp will still make it behave different on different frame rates though.

avatar image InvincibleCat · Jan 12, 2015 at 08:04 PM 0
Share

what ? that just makes no sense to clamp something that depends on deltaTime. Clamping here is not good too.

avatar image DoubleIsLoveDoubleIsLife · Jan 12, 2015 at 11:56 PM 0
Share

Yeah, I made this script on a few hours of sleep so I did some errors, I'll replace the code with the working code.

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

7 People are following this question.

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

Related Questions

SmoothDamp smoothTime ignored! 1 Answer

How does Time.deltaTime provide smoother physics functions? (Frame rate question) 2 Answers

Is there any way to determine Unity's actual target frame rate? 1 Answer

I can't get framerate independence to work... 1 Answer

Mathf.Lerp happens instantly 2 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