I am trying to shoot projectiles from a moving object. Picture a floating turret following and rotating around the player. This game is in 3rd person. The crosshair is controlled by the player and the turret is suppose to shoot where the player is pointing. In the playercontroller I have the crosshair set up and a raycastHit that gives me the object and location of where I want to shoot. Then that script calls the script below. the turret shoots but only forwards, never at the object I want it to. Any help would be awesome. Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Navi_Shoot : MonoBehaviour
{
public GameObject projectile;
public GameObject spawnLocation;
public float t = 5f;
public void Shoot(Transform target)
{
GameObject bullet = Instantiate(projectile, spawnLocation.transform.position, projectile.transform.rotation) as GameObject;
bullet.SetActive(true);
bullet.GetComponent<Rigidbody>();
Vector3 curLocation = bullet.transform.position;
//set up rotation using quaternion
bullet.transform.rotation = Quaternion.RotateTowards(bullet.transform.rotation, target.rotation, t);
//now apply force to go that direction
bullet.GetComponent<Rigidbody>().AddForce(Vector3.MoveTowards(curLocation, target.transform.position, t));
Destroy(bullet, 10);
}
}