- Home /
How to make a crosshair move with the player
I have a crosshair image on my canvas but I don't know how to get it to follow the player's position. I was told that I could use raycast, but how would I go about changing the crosshair's position according to the raycast of the player?
What do you mean move with the player? Like stay in front of the player in the center of the screen at all times?
If so then you just set its position to 0, 0, 0 and it will stay in the center of the screen.(With the new UI)
Answer by bracciata · Jul 27, 2015 at 12:16 AM
I like to use this javascript. All you have to do is attach it to the crosshairs and set target as player. Although it doesn't use raycast it works well. Also if it doesn't line up to the exact spot you want it to you could add + 1 to the end of a line or whatever you please.
#pragma strict
var target: Transform;
function Update () {
transform.position.x = target.position.x ;
transform.position.y = target.position.y ;
transform.position.z = target.position.z;
}
Your answer
Follow this Question
Related Questions
Canvas Graphics raycaster, prevent 2d and 3d physic raycast 0 Answers
Click on Canvas in a render texture 0 Answers
Can't interact with buttons in a UI Canvas in World Space Render Mode. 0 Answers
Random Raycast inside crosshairs 1 Answer
UI Buttons visible but not clickable when using two canvases. 3 Answers