Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

c# - Is it necessary, and if so, where can I add Time.deltaTime?

I want to add Time.deltaTime somewhere, but I can't quite figure out where. Everything works as it is, but to ensure equal movement between all computers and fps, Time.deltaTime seems necessary.

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    //================= MOVEMENT =====================
    public float moveSpeed = 10f;
    public Rigidbody2D rb;
    private Vector2 movePosition;

    //Aiming
    public Camera cam;
    Vector2 mousePos;

    // Update is called once per frame: Good for INPUTS
    void Update()
    {
        //movement
        float moveX = Input.GetAxisRaw("Horizontal");
        float moveY = Input.GetAxisRaw("Vertical");
        movePosition = new Vector2(moveX, moveY).normalized;

        //aiming
        mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    }
    // Called Set amount of times. Good for PHYSICS CALCULATIONS
    void FixedUpdate()
    {
        Move();

        //aim
        Vector2 lookDirection = mousePos - rb.position;
        float angle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg - 90f;
        rb.rotation = angle;
    }

    void Move()
    {
        rb.velocity = new Vector2(movePosition.x * moveSpeed, movePosition.y * moveSpeed) ;
    }
}
question from:https://stackoverflow.com/questions/65885636/is-it-necessary-and-if-so-where-can-i-add-time-deltatime

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

tl;dr -> You don't need it! Look at the bottom of this answer


Time.deltaTime is the time passed since the last frame was rendered.

=> for e.g. 60 F/S this value will be around 0.017 => you need 60 of it in order to reach a factor of 1.

So in short by multiplying a value with Time.deltaTime it converts it from value per frame into value per second.


Is it necessary

Depends on your use-case ... do you want a fix value per frame (frame-rate dependent) or do you rather want a value per second (frame-rate independent).

Of course there are things you want to do on a per frame base like e.g. interpolating position inputs from an eye-tracker in order to stabilize the input but in general frame-rate dependent is usually a bad thing.

For something like

transform.position += transform.forward * moveSpeed;

it means

  • If you have a more powerful device => more frames can be rendered in the same time => You move faster
  • If you have a slow device => less frames can be rendered in the same time => You move slower

where can I add Time.deltaTime

Well, it is a float so you can add it wherever you want and can multiply something by a float ;)

In your specific use-case: YOU DON'T. The Rigidbody.velocity

rb.velocity = movePosition * moveSpeed;

already is an absolute velocity in Unity units per seconds, so there is no need to multiply by Time.deltaTime for this!

Just as a counter example a use-case where you would need it would be using this instead

rb.MovePosition(rb.position + movePosition * moveSpeed * Time.deltaTime);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...