The main goal of my code is to make my turret aim at the location that the camera is pointing at, I have used the hit.point information of a raycast function to find the position of where the camera is pointing.
The ray hit position updates just fine in the camera aim script but does not update at all in the turret script. In the turret script, the target info remains as 0,0,0 while in the aim script it updates rapidly.
Here is the script code, I have put some of my former attempts in comment format.
{
public GameObject turret;
public GameObject camera;
public float rotationSpeed = 10f;
[SerializeField] private Vector3 targetPointInfo;
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
TurretAimPointer aimpainter = camera.GetComponent<TurretAimPointer>();
aimpainter.target = targetPointInfo;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetPointInfo), rotationSpeed * Time.deltaTime);
//TurretAim();
}
private void TurretAim()
{
//TurretAimPointer aimpainter = camera.GetComponent<TurretAimPointer>();
//aimpainter.target = targetPointInfo;
//transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetPointInfo), rotationSpeed * Time.deltaTime);
//Vector3 relativePosition = aimpainter.target - transform.position;
//Quaternion rotation = Quaternion.LookRotation(relativePosition, Vector3.up);
//transform.rotation = rotation;
}
}
{
// Start is called before the first frame update
private Camera cam;
public Vector3 target;
void Start()
{
}
// Update is called once per frame
void Update()
{
TurretAimPaint();
}
public void TurretAimPaint()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(transform.position, Vector3.forward, out hit))
{
target = hit.point;
}
}
}
These two images show the difference in the hit point info of the two scripts

