Hola, estoy tratando de asignar += o -= a una posición de objeto. En primer lugar, tengo matemáticas aleatorias eligiendo un número:
var min = 9; var max = 11; var pos = Math.floor(Math.random() * (max - min + 1)) + min;Estoy tratando de agregar el resultado de pos a this._target.position.z y this._target.position.x pero quiero que se agregue o reste al azar
por lo que el resultado aleatorio debería ser para la posición Z debería ser:
this._target.position.z += pos;o
this._target.position.z -= pos;y luego para la posición X debería ser:
this._target.position.x += pos;o
this._target.position.x -= pos;gracias de antemano.
Si desea hacer aleatoriamente x += value o x -= value , simplemente puede usar la primera versión y luego multiplicar su valor por -1 si desea hacer un -= .
En otras palabras:
const isNegative = // however you determine this, eg. Math.random const newValue += (isNegative ? -1 : 1) * value;O, para volver a aplicarlo a su caso, usando la sugerencia de @Chris G en los comentarios:
const oneOrNegativeOne = (Math.floor(Math.random() * 2) * 2 - 1); this._target.position.x += oneOrNegativeOne * pos;Puede usar una condición basada en un evento aleatorio como este:
if (Math.floor(Math.random() * 2)) { //the condition return 0 or 1 for false or true this._target.position.z += pos; } else { this._target.position.z -= pos; } y ya sea para this._target.position.x