0

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

enter image description here

enter image description here

1 Answer 1

1

Well you do

aimpainter.target = targetPointInfo;

So for a split moment you overrule the target with the value of targetPointInfo before the aim script then resets it again in the next Update call.

I'm pretty sure you rather wanted to do

 targetPointInfo = aimpainter.target;

so fetch the value from the aim script and use it for the other one!

(Little typos btw aimpainter => aimpointer - same in your method TurretAimPaint => TurretAimPoint)


Actually personally I wouldn't even introduce any additional field for this at all. It only introduces redundant complexity - and as you saw is error prone.

Just use a single source of truth and go through the aimpainter.target directly - there is no need to first copy the value to another field.

I would even make the target readonly from the outside so you can be sure nothing accidentally overwrites it

public Vector3 target { get; private set; }

This converts it to a property that is public for reading the value but only your script itself can set the value.

And then do

// already assign this via Inspector instead of the camera
[SerializedField] TurretAimPointer aimPointer;

// you sure you want to use FixedUpdate for this?
// This is usually rather reserved for physics related things 
// If you just wanted to be sure the other Update is called first
// you could use LateUpdate here
private void FixedUpdate()
{
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(aimPointer.target), rotationSpeed * Time.deltaTime);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.