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.
1 Answer
1to 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); } } }
– jonathangreenberg
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.
– oliver82957Tried it out, sadly did not work and I think that crosshair should move with the player/ barrel of the gun.
– oliver82957