- Home /
how to create a old (2D style) fps cursor
i want to create a mouse cursor that can move on screen and shoot bullets with it. i want something like classic nes 2d fps controls. i don't want and can not use OnMouseDown. how can i detect what object is under the mouse with raycasting? i thought i should use the field of view and position of the mouse and it's difference from center to calculate the angle of the ray but i could not make any working component. i just need to know what is under the mouse cursor and then shoot something toward it.
Personally, it's not entirely clear to me what you're asking. I think it's - the mouse is supposed to move a targetting cursor around a 2D screen. Then, when some other event causes a bullet to fire (pressing spacebar?) the bullet is given a GameObject target, that happens to be under the mouse at the time of firing. And what you're asking is, how to deter$$anonymous$$e what that GameObject is, without any extra actions on the player's part (such as mouse-clicking).
there are functions like camera.ScreenPointToRay and ScreenToWorldPoint that can help you find the point in world that mouse is located there. but because the camera view is a cone you can not simply find the position of mouse in far points from camera with strate lines.
Again I'm confused - your title and text say 2D, then you're talking about World coordinates and view cones. Is this a 2D game or 3D game?
the game is 3d. by 2d i mean a cursor like 2d games of nes. do you remember those games that you could move the cursor with game controller on the screen. i want to move my cursor on the screen.
Answer by Horsman · Apr 08, 2010 at 08:02 PM
Camera.ScreenPointToRay is your friend in this case http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay.html
Remember that unlike UnityGUI, the screen coordinates are from bottom left, in pixels.
this is not what i want because the ray's direction is incorrect. you can draw that ray to see what i mean. the real position of each object has some difference and the ray needs to rotate in x and y direction.
This is not the case. Using this function, the ray should cast in the correct direction. Please explain to me how the ray is not in the correct direction. This might be the case if you are using an orthoganal(isometric) projection.
you are right. i just wrote it incorrectly. i even found a note on my notes from 3 months ago. i used this function before but i forgot it and now i don't want to use it. do you know the formulla of calculating the direction without using the ray?
Answer by Horsman · Apr 15, 2010 at 04:58 PM
to find the direction without the ray, you can use a number of methods:
first, find the screen co-ordinates using Camera.ScreenToViewportPoint ( http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToViewportPoint.html ) . This will give you values from 0 - 1, bottom left to top right.
Then calculate the angular deviation from the center of the screen using the Field of View and the aspect ratio. For the y direction the calculation looks like:
deviationy = (ViewportPt.y - 0.5) * fovy deviationx = (ViewportPt.x - 0.5) * fovy * aspect
endpoint = Cam.forward farClip + Cam.right cos(deviationx) + Cam.up * cos(deviationy)
rayDirection = (endpoint - camera.pos).magnitude
unfortunately i elected the best answer before. i thought that i can change it but i can not change it. sorry. but don't worry, you helped me so you'll be helped somewhere at sometime. it's the most beautiful rule in our world.
oh you are the guy that i elected before so no problems :) thank you again
Answer by 3ddf · Apr 14, 2010 at 11:28 AM
Trying to work out what you mean here... So, for example you would move your cursor around the screen and over a target, then press space to fire a bullet from your gun ( positioned elsewhere on the screen? ) to the target under your cursor?
So you'd cast a ray from current mouse position to detect if anything is there, then work out the angle from your gun to the target, then fire along that angle. Something like the following...
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); //fire a ray from current mouse pos var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) { //if the ray hits something... if (hit.transform == yourTarget){ //if that something is what you want to target var targetAngle = Vector3.Angle(Vector3.up, yourTarget.transform.position - yourGun.transform.position); //work out the angle between your gun and the target //then rotate your gun to face target based on the above angle and instantiate your projectile } }
this is only a really rough outline of what might work, I don't have time to test sorry
Your answer
Follow this Question
Related Questions
Move camera with mouse (2D) 1 Answer
Get GameObject That Was Last Clicked? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Problem switching between controller and mouse input 1 Answer
OnMouseEnter/Exit problem 2 Answers