c# - Unity: Special movement for game -


i'm writing movement space game , spaceship object (player) mouse cursor.

currently have following code:

using unityengine; using system.collections; using unityengine.eventsystems;  public class move : monobehaviour {      public float speed = 1.5f;     public float rotationspeed = 90f;     public float rotprecision = 0.1f;     public float moveprecision = 0.1f;     private vector3 pos;     private quaternion qto;      void start () {         pos = transform.position;         qto = transform.rotation;     }      void update () {         if (!eventsystem.current.ispointerovergameobject())         {              if (input.getmousebuttondown(0) || input.getmousebutton(0))             {                 pos = input.mouseposition;                 pos.z = transform.position.z - camera.main.transform.position.z;                 pos = camera.main.screentoworldpoint(pos);             }              var dir = pos - transform.position;             qto = quaternion.lookrotation(vector3.forward, pos - transform.position);              if (quaternion.angle(transform.rotation, qto) >= rotprecision) //just set own precision                 transform.rotation = quaternion.rotatetowards(transform.rotation, qto, time.deltatime * rotationspeed);              if (vector3.distance(transform.position, pos) > moveprecision) // 0.1f                 transform.translate(vector3.up * speed * time.deltatime);         }     }      } 

but there have problem movement precision , rotation when point close player (have infinite loop).

the idea of movement system described following image:

(player actor green, path gray, , destination point red).

movement plan

i hope me w/ that.

thank you!

if understand question correctly, problem player's movement never stops code can't reach finishing point.

to solve can add acceptable precision margin.

so calculate if difference between rotation wish or movement wish, , players actual rotation/position, less given variable, example less 0.05%.

that way allow program know if it's within 0.05% precision, it's okay stop moving.

otherwise, if program never reaches complete , perfect rotation , position, continue adjust endlessly due slight mathematical imprecision in calculations , movement pattern.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -