How do I make a crosshair for a 3rd person shooter.

Hi, I am new to unity and have started a third person shooter game and was wandering if there was a way to align the UI with the Ray /game object so the crosshair can be accurate.

Didn't realize that it was that easy. I Just didn't know that you could link a UI position with mouse pointer. Well I'll try that and see if it works.

Tried it out, sadly did not work and I think that crosshair should move with the player/ barrel of the gun.

1 Answer

1

to align UI with mouse pointer i usually use

public Image crosshair; 
void Update(){
croshair.transform.position = Input.mousePosition;
}

but if you want to align a gameobject in world space to screen space use Camera.main.ScreenToViewportPoint(Input.mousePosition);

hope that helps

oh sorry that might be Camera.main.ScreenToWorldPoint(Input.mousePosition) one of those :D

@HHammerite I am sorry but i did that and now it is saying that "barrier1 does not exist in the current context". This does not make sense to me because i did everything right and barrier1 does exist in the scene. This has never happened to me before. Can you help? Here is the code just in case i messed up. using UnityEngine; public class MyExampleClass : MonoBehaviour { void OnCollisionEnter(Collision collision) { if(collision.gameObject.name == "barrierdestroyer1") { Destroy(barrier1); } } }