Your problem here is two fold:
First, when you spawn an object, that should be the last the current script interacts with it. From that point forward, the new object should control its own behavior. You need to modify your projectile to handle its own movement.
The second is the use of Vector3Vector2.MoveTowards() instead of Translate().
For projectiles that don't track the mouse, like a bullet:
using UnityEngine;
using System.Collections;
public class FixedProjectile : MonoBehaviour {
public Transform target;
public float speed=6.0f;
void OnStart()
{
target=Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
void Update() {
float step = speed * Time.deltaTime;
transform.position =Vector3=Vector2.MoveTowards(transform.position,target.position,step);
}
}
For projectiles that track the mouse, like a guided missile:
using UnityEngine;
using System.Collections;
public class MouseGuidedProjectile : MonoBehaviour {
public Transform target;
public float speed=6.0f;
void Update() {
target=Camera.main.ScreenToWorldPoint(Input.mousePosition);
float step = speed * Time.deltaTime;
transform.position =Vector3=Vector2.MoveTowards(transform.position,target.position,step);
}
}
For projectiles that track a specific target, like a guided missile with target lock, simply remove the target line from Update() :
using UnityEngine;
using System.Collections;
public class MouseGuidedProjectileGuidedProjectile : MonoBehaviour {
public Transform target;
public float speed=6.0f;
void Update() {
if(target!=null)
{
float step = speed * Time.deltaTime;
transform.position =Vector3=Vector2.MoveTowards(transform.position,target.position,step);
}
}
}
and set the target transform after you instantiate the object
GameObject missile = (GameObject)Instantiate(prefab, transform.position,Quaternion.identity);
missile.target=GameObjectToTarget.transform;