- Home /
Align the center of Camera towards a position in the world
I have a third-person view of an archer that shoot arrows. What I want to solve: I want the crosshair in the middle of the screen to follow the position my bow is aiming at, by rotation the camera so that the aim position overlaps with the crosshair.
I use a script on the bow object to find the aim position:
bow.cs
UPDATED WITH SOLUTION UNDER
private Vector3 aimAtPosition;
private void FixedUpdate() { RaycastHit hit; Vector3 transformDirection = transform.TransformDirection(Vector3.forward); if (Physics.Raycast(transform.position, transformDirection, out hit, 800)) { float distance = Vector3.Distance(hit.transform.position, transform.position); aimAtPosition = transformDirection * distance; return; } aimAtPosition = transformDirection * 800; }
public void getBowTarget() { return aimAtPosition; }
And then I use a script on the camera parent object to try and rotate towards it:
cameraParent.cs
transform.LookAt(bowScript.getBowTarget());
Crosshair:
void OnGUI() { GUI.DrawTexture(new Rect(Screen.width / 2,Screen.height / 2, 60, 60), crosshairTexture, ScaleMode.ScaleToFit, true, 10.0F); }
The rotation of the camera looks good, but the camera is not centering on the aimAtPosition
. It is not precise. Is there a better way than to use the LookAt
method to center the camera on a position in the world?
Answer by Bunny83 · Mar 18, 2020 at 04:36 PM
Uhm I don't see an issue with your lookat code. However you don't draw your crosshair in the center of the screen since you use this rect:
new Rect(Screen.width / 2,Screen.height / 2, 60, 60)
So your Rect starts at the screen center and extends 60 to the right and 60 downwards. You probably wanted to do
new Rect(Screen.width / 2 - 30,Screen.height / 2 - 30, 60, 60)
assuming that the center of your crosshair should be the actual center to aim at.
Of course since your camera and your bow ai$$anonymous$$g direction are most likely offset in a third person game, always looking at a position 50 units ahead might not represent the actual position you might hit. A closer position would result in a different hit location. The code that Hellium posted should help in that case.
@Bunny83 Thats right. I set the default distance to a higher value and use Raycast to adjust it if Im targeting something closer to me. I will update my question with the solution. Thanks :)
Answer by Hellium · Mar 18, 2020 at 03:34 PM
Following code not tested:
// Bow.cs
public Vector3 Target { get ; set ;}
private void FixedUpdate()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 50))
{
Target = hit.point;
}
else
{
Target = ray.GetPoint(50);
}
}
// CameraParent.cs
transform.LookAt(bowScript.Target);
// Crosshair.cs
void OnGUI()
{
GUI.DrawTexture(new Rect(Screen.width / 2,Screen.height / 2, 60, 60), crosshairTexture, ScaleMode.ScaleToFit, true, 10.0F);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612225216im_/https://answers.unity.com/themes/thub/images/avi.jpg)