diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 3f4adde462..2ba6305211 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -2189,7 +2189,19 @@ class Vector { */ setHeading(a) { if (this.isPInst) a = this._toRadians(a); - let m = this.mag(); + if (this.y === undefined || (this.z !== undefined && this.z !== 0)) { + const p5inst = this.isPInst ? this._pInst : (typeof p5 !== 'undefined' ? p5 : null); + if (p5inst && p5inst._friendlyError) { + p5inst._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(); this.x = m * Math.cos(a); this.y = m * Math.sin(a); return this;