Projectile

Hey, I am trying to get my turrets to fire arrows at enemies which collide with it’s boundaries. So far I have managed to get my LookAt function working properly so that the arrow faces the target but I have been fairly unsuccessful it getting the arrow to move towards its target. All I have been getting is compiler errors :@

public class ArrowProjectileScript : MonoBehaviour {

public GameObject Target;
public Transform target;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;

 
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		Target = GameObject.FindWithTag("Enemy");
		
		GameObject turret = GameObject.Find("Sphere");
		TurretScriptTest ts = turret.GetComponent<TurretScriptTest>();
		
		if(ts.hit == true)
		{
			target = Target.position;
		transform.LookAt(Target.transform);
		Vector3 targetPosition = target.TransformPoint(new Vector3(-75, 30,30));
		transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
		}
	}
}

If you think you can solve this problem please help!

I have managed to get the arrow to fire now, but it seems to fly around the enemy rather than directly at it!

public class ArrowProjectileScript : MonoBehaviour {

public GameObject Target;
private Vector3 direction = Vector3.up;
public float speed=10.0f;

 
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		Target = GameObject.FindWithTag("Enemy");
		
		GameObject turret = GameObject.Find("Sphere");
		TurretScriptTest ts = turret.GetComponent<TurretScriptTest>();
		
		if(ts.hit == true)
		{
		transform.Translate(direction*speed*Time.deltaTime);
		transform.LookAt(Target.transform);
		}
	}
}

This may be that your speed is higher then the distance to the target thus it overshoots on a frame and if its turnspeed is not fast enoth it will orbit the target.

Instead of seeking the target you can arrive at the target.

use Mathf.Min(speed, distance) for transform.Translate(directionspeedTime.deltaTime);
This should check the distance and if its to close it will go directly to the position instead of overshooting it.

Also google Steering Behaivors.

thanks buddy, turns out all i had to do was change the direction to Vector3.forward! i had Vector3.up (don’t ask why!)