- Home /
How do you find an object in the middle of the screen?
I am making(or learning to make one) a game and i need to find the object in the middle of the screen and the rotate an object(a bullet) in its direction. My question is how to find the object which will be in the middle of the screen ?
$$anonymous$$ay you be more specific in you question? When you say middle of the screen, do you mean as in the actual middle of the screen or do you mean middle of a level? What I would suggest is to use some sort of raycast to be able to at least identify the object if you're doing a first person shooter. Here's a link to a video that shows a bit more about raycasts and shooting something in Unity. https://www.youtube.com/watch?v=THnivyG0$$anonymous$$vo Best of luck :)
Answer by arbazgillani · Oct 02, 2018 at 06:11 AM
Hit a raycast from the middle of the camera and detect if it is in the middle. This code may Help you.
function Update() { var x = Screen.width / 2; var y = Screen.height / 2;
var ray = camera.ScreenPointToRay(Vector3(x, y, 0));
Debug.DrawRay(ray.origin, ray.direction * 1000, Color.yellow);
}
Your answer
Follow this Question
Related Questions
l want my game object to appear in a location and change when screen size changes? 0 Answers
2D rotation around a object 4 Answers
How do I fix my player from spinning? 1 Answer
Is it possible to constrain an object's rotation in worldspace? 2 Answers
Rotate character to the moving direction problems? 2 Answers