- Home /
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);
     }
 }
use FixedUpdate - and Time.fixedDeltaTime to eli$$anonymous$$ate the frame rate issue
using these will always result in 1 being 1 second
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.
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.
@$$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".
@Eric5h5 not totally agree with the fixed update thing. FixedUpdate should be used in this case for the movement Update part thought
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
The deltaTime in Lerp will still make it behave different on different frame rates though.
what ? that just makes no sense to clamp something that depends on deltaTime. Clamping here is not good too.
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
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                