Move GameObject In A Direction

Hello! Im a newbie in unity, and have only been programming for like a year and a half so not that experienced.

Anyways… Im working on a school project with some classmates, its going to be a moba, similiar to lols all random all mid.

So… I have a character which throws a spikeball when I press a button, and the ball is moving towards the mouse position that was when I pressed the “fire” button.

I simply use Vector3.MoveTowards for this, and pass in the initial mouse position coordinates.

Now the problem that this only makes the ball go to the mouse position, when it has reached the mouse position (or “targetPosition”) it just stops and falls down to the plane.

What I want is that my gameobject should move in the direction of the mouse position, and just continue moving in that same direction once it has reached the mouse position.

I hope this makes sence, english isnt my 1st language and if there’s something that you dont understand, please tell me and I will try to explain it better.

Now if some awesome person could point me in the direction of some helpfull methods, or write some code for me, that would be extremely helpfull.

EDITED: This is the script that is attached to the “spikeball”.

public float lifetimeSeconds = 2f;

private float _projectileSpeed = 5f;

private Vector3 targetPosition;

void Start () {

targetPosition = GetMousePosition ();	 
}


void Update () {

    this.gameObject.transform.position =  Vector3.MoveTowards(this.gameObject.transform.position, 
                                                             targetPosition,
                                                             _projectileSpeed * Time.deltaTime);

    Destroy(gameObject, lifetimeSeconds);
}

private Vector3 GetMousePosition(){

    Plane playerPlane = new Plane(Vector3.up,
                                  this.gameObject.transform.position); 

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
    float hitdist = 0.0f; 
    Vector3 mousePosition = Vector3.zero;
		 
    if (playerPlane.Raycast (ray, out hitdist)) {
	mousePosition = ray.GetPoint (hitdist);
    }
 
    return mousePosition;
}

Thanks // Forskan.

Given that ‘targetPosition’ is a Vector3 and therefore will not change over time, the easiest solution is a hack…move your target position beyond the target. In the case of your code above, you could do it this way:

void Start () {
    targetPosition = GetMousePosition ();
    targetPosition = transform.position + (target.Position - transform.position).normalized * 1000.0f;    
}

Another common solution is to move the object forward. So your code in Start() would be:

void Start () {
    targetPosition = GetMousePosition ();
    transform.LookAt(targetPosition);
}

Then line 15 becomes:

transform.Translate(0.0f, 0.0f, _projectileSpeed * Time.deltaTime);

The third solution is to establish a world direction and move the object in that direction. So Start() becomes:

void Start () {
    targetDirection = (GetMousePosition() - transform.position).normalzied;    
}

Then your movement code would be:

transform.position += targetDirection * _projectileSpeed * Time.deltaTime;

Note I’ve changed the name of the variable from ‘targetPosition’ to ‘targetDirection’.