Using MoveToward but is no working.

every time i shoot, it wont shoot to the mouse position.

it only shoot forward from the shooting point.

my friends have use this movetoward and it work, what is the error of my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwipeTrail : MonoBehaviour {

	public Rigidbody bullet;
	public Transform firePoint;
	public float speed;

	// Touch.Began - when first press the finger in the screen
	// Touch.Ended - when lifted up the finger from the screen
	void Update () 
	{
		speed = speed * Time.deltaTime;
		// if the touching is more than 0, the touch pahse is Moved (when sliding the finger across the screen
		if ((Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) || Input.GetMouseButton (0)) {
			Plane objPlane = new Plane (Camera.main.transform.forward * -1, this.transform.position);
			Ray mRay = Camera.main.ScreenPointToRay (Input.mousePosition);
			float rayDistance;
			if (objPlane.Raycast (mRay, out rayDistance)) {
				this.transform.position = mRay.GetPoint (rayDistance);
				Rigidbody rocketInstance;
				rocketInstance = Instantiate (bullet, firePoint.position, firePoint.rotation) as Rigidbody;

				rocketInstance.transform.position = Vector3.MoveTowards (firePoint.position, this.transform.position, speed);
			}
		}
	}

//	void OnTriggerEnter(Collider col)
//	{
//		Debug.Log ("haha");
//		if (col.tag == "mons") {
//			Destroy (col.gameObject);
//		}
//			
//	}
}

Vector3.MoveTowards is a very simple function with no magic attached.

What it does:

it takes two Vector3s a and b and a float as a distance. It returns a new Vector3 that is “distance” meters away from a, towards b.

If the distance between a and b is shorter than the given distance, it will return b.

What it does NOT:

  • Change any object’s position, or change anything in your scene in general
  • Remember what it did last time it was called
  • Do something over some amount of time

So… you’re using the function once every time you hit something with your touch-based ray. The moment that happens, the object will be placed somewhere between firePoint.position and transform.position. And then that’s it, the object stays there.

If you want a thing to constantly move, you need to call a function like MoveTowards in every frame until the destination is reached. This works in Update, but Coroutines also work well.

Good day.

Do you recieve any error when shoot?

Explore to be sure all parameters are correct before execute each function, like this:

Debug.Log (firePoint.position);
Debug.Log (this.transform.position);
Debug.Log (Speed);

rocketInstance.transform.position = Vector3.MoveTowards (firePoint.position, this.transform.position, speed);

So you will be able to see in the console if all paratmeters are good registered before executing the comand.

And as i see from the scripting API, the paramteres must be (Origin position, Destination position, MaxDistance float) and i think you have them swaped.

If correct, accept the ansewr!

Bye!