diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 97b72ab61b..50a3b695d5 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -2191,7 +2191,21 @@ class Vector { */ setHeading(a) { if (this.isPInst) a = this._toRadians(a); - let m = this.mag(); + if (this.z !== 0) { + p5._friendlyError( + 'p5.Vector.setHeading() only supports 2D vectors (z === 0). ' + + 'For 3D or higher-dimensional vectors, use rotate() or another ' + + 'appropriate method instead.', + 'p5.Vector.setHeading' + ); + return this; + } + const m = this.mag(); + if (m === 0) { + this.x = 0; + this.y = 0; + return this; + } this.x = m * Math.cos(a); this.y = m * Math.sin(a); return this;