samedi 2 avril 2022

Integrating random values in Unity3d works incorrectlly

I have simple script:

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

public class DLATEST : MonoBehaviour
 {
     int wx = 0;
     
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         wx += Random.Range(-1, 1);
         
         Debug.Log(wx);
     }
 }

but I have weird results: 0 -1 -2 -3 -4 -5 -6 -7 -8

wx slowly declines to negatives until it overflows.

I expect that wx will have values near 0 and slowly change around it, but not decline as it have. This is simple numerical integration but it works incorrectly.

I have also tried this code:

int wx = 0;
int wxPrev = 0;
 
 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     wx = wxPrev + Random.Range(-1, 1);
     
     Debug.Log(wx);
     wxPred = wx;
 }

Results are the same.

If I debug Random.Range(-1, 1) only then everything is ok, random values are random.

What is wrong in my code? Thank you.




Aucun commentaire:

Enregistrer un commentaire