Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to calculate time in x/v=t

Discussion in 'Scripting' started by kader1081, Aug 24, 2023.

  1. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    Hi, how can i calculate time respect to velocity and distance i am using this code but time is unrelated . The problem is Debug.Log("floattimeRaw " + floatTime); is floattimeRaw 468410,6 but Debug.Log("dest " + timePassed); is dest 0,7153126
    Code (CSharp):
    1.  
    2.   public GameObject enemy;
    3.     public GameObject explosion;
    4.     public bool isParabolics;
    5.     public Color craterColor = Color.black;
    6.     float maxCollapseDepth = 3f;
    7.     float time;
    8.     float GeneralTime;
    9.     float craterSize = 5f;
    10.     Rigidbody rig;
    11.     float timer = 100f;
    12.     public int damage;
    13.     Vector3 speed;
    14.     float floatTime;    // divide the 189 to float time for how much time will it take to hit and divide floatTime to 1000;
    15.     float verticalSpeed = 600f;
    16.     float horizontalSpeed=800f;
    17.     float timePassed;
    18.     void Start()
    19.     {
    20.         rig = GetComponent<Rigidbody>();
    21.         speed = rig.velocity;
    22.         if (isParabolics)
    23.         {
    24.             Vector3 horizontalDirection = enemy.transform.position - transform.position;
    25.             floatTime = horizontalDirection.magnitude / horizontalDirection.normalized.magnitude * horizontalSpeed;
    26.             Debug.Log("floattimeRaw " + floatTime);
    27.          //   Debug.Log(floatTime);
    28.             Vector3 velo = horizontalDirection.normalized * horizontalSpeed;
    29.          //   velo.y += verticalSpeed;
    30.             rig.velocity = velo ;
    31.         }
    32.     }
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         if (isParabolics)
    37.         {
    38.            
    39.             // Vector3 speedo = rig.velocity;
    40.             timePassed += Time.deltaTime;
    41.             // wholeSpeed = verticalSpeed - (((Time.deltaTime / floatTime)) * verticalSpeed);
    42.             //  speedo.y -= (((Time.deltaTime  / floatTime)) * verticalSpeed);
    43.             //rig.velocity = speedo;
    44.         }
    45.      
    46.             timer -= Time.deltaTime;
    47.             if (timer<=0f) Destroy(gameObject);
    48.             // Guide();
    49.     }
    50.     private void OnDestroy()
    51.     {
    52.             Debug.Log("dest  " + timePassed);
    53.     }
    54.  
    55.     }
     
  2. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    İ think i found the problem in line 25 i should make it floatTime = horizontalDirection.magnitude / (horizontalDirection.normalized.magnitude * horizontalSpeed); i have wasted 2 hours for this if this is the problem i hate my life
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    As expected, dx/dt gives you instantaneous velocity.

    Obviously it is meaningless if dt == 0

    The closer dt comes to 0 the more unstable it is, just due to dividing by very small values with single precision values.

    Changes in dt (eg, uneven framerates) may also introduce drift / jitter / noise.

    If you have the velocity from the rigidbody, use that. It will usually be less noisy.
     
  4. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    do you mean i should divide by rigidbody velocity even it is same velocity
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This doesn't make sense. What are you trying to do here?

    Physics doesn't change however. Always turn to basic kinematics when in doubt.
     
  6. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    i fixed the problem the problem was brackets
    1. floatTime = horizontalDirection.magnitude / horizontalDirection.normalized.magnitude * horizontalSpeed;
    2. floatTime = horizontalDirection.magnitude / (horizontalDirection.normalized.magnitude * horizontalSpeed);