Special movement for game

Hi there!

I’m writing movement for my space game and spaceship object (player) with 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 your 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 i have the problem with the movement precision and rotation when the point is too close to player (have infinite loop).

The idea of this movement system described with the following image:

(Player actor is green, path is gray, and destination point is red).

Unity has a great community and I hope that you could help me w/ that.

Thank you!

hi;
u have a “rotationSpeed” value;
if u increase this value this problem will be solved cause the space ship rotate too fast and it wont stuck in an infinite loop;
but that does not look good when space ship is far from the red point ;

so the idea here can be to get the distance between the player and the red point and multiply the rotaition of the space ship based on that distance;

     float t = Vector3.Distance(Playerpos, Redpoin);


  transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * rotationSpeed / t);

so with the code above “t” is the distance between them and if its a big number the rotaition will be so low but if its a small number rotation will be faster;
i wrote this to give u an idead but u have to play with the values to find a good value for the space ship rotation;