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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Why do other unrelated parameters fluctuate minutely?

Discussion in 'Scripting' started by whi7es7one, Oct 21, 2023.

  1. whi7es7one

    whi7es7one

    Joined:
    Oct 1, 2023
    Posts:
    8
    I'm completely new to Unity and C#.
    I'm using Google Translate to ask the question, so please forgive me if my sentences may be weird.

    Why do other parameters other than the z-axis move slightly while running the code below?
    I can understand that there will be a slight difference in the z-axis, but I don't understand why the x- and y-axes are affected.

    upload_2023-10-21_16-6-42.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerControl : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         //Move the vehicle forward
    17.                 transform.Translate(Vector3.forward * Time.deltaTime * 20);
    18.     }
    19. }
    20.  
     
  2. Deleted User

    Deleted User

    Guest

    I'll let others who know better answer the question. My best guess would be that this happens because of the Vector3 used in the code line and the fact that it uses floats not integers.
    Welcome to Unity! ;)
    Edit: what is your mother tongue?
     
    whi7es7one likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,586
    Is your vehicle object a child of something else? That would be my guess. Though the difference is just 0.00001 and 0.0000002, so almost 0. So without more background we can't really tell.
     
    whi7es7one likes this.
  4. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,698
    Google floating point precision, its normal.
     
    Bunny83 and whi7es7one like this.
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,271
    Translate may be a matrix operation which means all axis are affected even the ones that don‘t change. And what you see fluctuating is precision issues due to floating point having a limited, not infinite, accuracy as others pointed out.

    The same precision issue causes all kinds of weird snapping or offset issues for transforms, animations, a
    shadefs, etc to varying degrees when an object is 5000+ units away from origin.
     
    Bunny83, whi7es7one and Deleted User like this.
  6. whi7es7one

    whi7es7one

    Joined:
    Oct 1, 2023
    Posts:
    8
    thank you everyone!
    Vehicles are not child.
    Unrelated axes also change slightly due to floating point and matrix operations!
    I was convinced!

    My native language is Japanese:)
     
    Deleted User likes this.
  7. Deleted User

    Deleted User

    Guest

    素晴らしい!私は日本語を勉強しています。

    (Translation: Great! I'm learning Japanese.)
     
    whi7es7one likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,093
    Bingo. By default it is also
    relativeTo = Space.Self
    so that means it is multiplied by the matrix before being added to the position, same as if it was translated by
    transform.forward
    .

    I really love that OP used a translator to report problems with transform.Translate()... more proof that we live in a simulator. :)

    Your sentences are fine! Welcome!
     
    whi7es7one likes this.
  9. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    619
    I don't believe it's Translate or floating point precision. I did a little test and I got zero deviation on the x and y. The only thing I could do to recreate this was to place the script on a cube with a rigidbody. And even then I only get slight deviation on the y axis. It wasn't until I enabled gravity and let the cube slide along the "ground" that I started to see obvious deviation.
     
    whi7es7one likes this.
  10. Deleted User

    Deleted User

    Guest

    Rigidbodies tend to do that but the op didn't mention that. And his code suggests no rigidbody involved.
     
    whi7es7one likes this.
  11. whi7es7one

    whi7es7one

    Joined:
    Oct 1, 2023
    Posts:
    8
    Rigidbody is enabled.
    Does this affect the fluctuation?

    upload_2023-10-22_9-51-57.png
     
  12. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    619
    When scripting a rigidbody you're supposed to use methods like AddForce().
     
    Last edited: Oct 22, 2023
    whi7es7one likes this.
  13. Deleted User

    Deleted User

    Guest

    Last edited by a moderator: Oct 22, 2023
    whi7es7one likes this.
  14. whi7es7one

    whi7es7one

    Joined:
    Oct 1, 2023
    Posts:
    8
    I see!
    After deleting the rigid body, values other than the Z axis no longer change.
    Thank you very much! ! ! !

    upload_2023-10-23_0-18-45.png