Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your problem is that the target could be at angle 5, and the object could be facing 355 degrees (for example). According to your test, 5 is less than 355, so go anticlockwise.</p> <p>What you should do is test whether the target is within 180 degrees to your left, or within 180 degrees to your right, then move accordingly.</p> <p>The tricky part is getting the check to 'wrap' around 360 &lt;-> 0. It looks like 0 degrees is left in your case, so the hard test is for when the wantRot is on the side that has 0 degrees within it.</p> <p>To visualise draw a circle as below, then place your object on the left of where we're facing. You'll see that you have to check the 2 shaded areas separately.</p> <h2><img src="https://i.stack.imgur.com/gkzeP.png" alt="Visualisation"></h2> <h2>Method 1</h2> <p>Check all cases separately.</p> <p><strong>Note:</strong> Code below is in my head and untested. You'll need to change degrees to radians.</p> <pre><code>int MoveDir = 0; var BehindMe = this.rotation - 180; if (BehindMe &lt; 0) BehindMe += 360; if (wantRot != this.rotation) { if (wantRot == BehindMe) MoveDir = 1; // or randomly choose else if ((wantRot &gt; BehindMe &amp;&amp; wantRot &lt; this.rotation) || (this.rotation &lt; 180 &amp;&amp; (wantRot &gt; BehindMe || wantRot &lt; this.rotation))) MoveDir = -1; else if ((wantRot &lt; BehindMe &amp;&amp; wantRot &gt; this.rotation) || (this.rotation &gt; 180 &amp;&amp; (wantRot &lt; BehindMe || wantRot &gt; this.rotation)) MoveDir= 1; this.rotation += MoveDir * MathHelper.ToRadians(45) * Time.deltaTime; } </code></pre> <hr> <h2>Method 2</h2> <p>From looking at the image, you may realise that you could just check whether the object on the right, then if not, assume it's on the left (since as long as the current angle is less than 180 degrees checking its on the right is easy). If the current angle is more than 180 degrees, then reverse the concept - check whether it's on the left and if not assume right. Something like below:</p> <pre><code>int MoveDir = 0; var BehindMe = this.rotation - 180; if (BehindMe &lt; 0) BehindMe += 360; if (wantRot != this.rotation) { if (this.rotation &lt;= 180) { if (wantRot &gt; this.rotation &amp;&amp; wanrRot &lt; BehindMe) MoveDir = 1; else MoveDir = -1; } else { if (wantRot &lt; this.rotation &amp;&amp; wanrRot &gt; BehindMe) MoveDir = -1; else MoveDir = 1; } this.rotation += MoveDir * MathHelper.ToRadians(45) * Time.deltaTime; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload