• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by sml79569 · Nov 12, 2013 at 04:07 AM · camerathird-personaimflight

Aiming with a third person camera/positioning a third person camera

I am working on a 3D space combat game where I have first and third person cameras. For the third person camera, it is slightly above and behind the ship. When the ship changes direction, the camera points in that direction, similar to Star Wars Battlefront II space combat. The question is how to provide accurate aiming with crosshairs. Raycasting won't work because you have to lead shots so hit moving targets.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Nov 12, 2013 at 04:57 AM

The problem with your setup is triangulation.

alt text

The orange hex represents the screen...that is the start of the ray into the 3D space. The blue triangle is the ship or gun or spawn point that you need to point to fire the projectile. Given a ray, there are an infinite number of places along the ray that the ship/gun/point can be aimed, and how you solve it will depend on your game.

If most of the action happens around a certain distance in front of the ship, then you can just pick a distance along the ray for the aiming to take place. You can use Camera.ScreenToWorldPoint(). The 'Z' parameter of the passed Vector3 will be the distance in front of the camera plane to calculate the point.

If your targets can be at greatly varying distances then you will need to pick an object to use to calculate the distance. You can find the nearest object to the mouse position by using Camera.WorldToScreenPoint() to convert each potential target a screen coordinate. You can then find the target with the least distance to the mouse position. The distance between the camera and that target is a good approximation to use for the distance in your Camera.WorldToScreenPoint() calculation.

If you need a more accurate calculation, then you can use Unity's mathematical Plane and use Plane.Raycast() against that plane. A mathematical plane is constructed using a point and a normal to the plane. For your geometry here, you would construct the plane using the point of the target and the 'forward' of the camera.


triangulate.png (14.0 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sml79569 · Nov 12, 2013 at 06:45 AM 0
Share

Thanks for the detailed answer. I see what you are saying. It seems that approximations are required. I think I will try out the various suggestions to see what works best, but it seems trial and error is the best bet here.

avatar image
1

Answer by TomMadden · Jun 12, 2016 at 02:21 PM

I've had the exact same problem. The "player camera" is raised above and behind my spaceship so any targeting reticule in the screenspace would be misleading.

I solved it by having a "Targeting camera" setup inline with the space ship. Set "Clear Flags" to "Depth Only" and "Culling Mask" to "Nothing". Both cameras are set up via a script to orbit the ship when the mouse moves but the player camera orbits a point above the ship and the targeting camera orbits the actual ship. Viewing through the Targeting camera would mean that the targeting would be accurate but the ship would always obscure the target.

I then created a Canvas element with the render mode set to "world space", the Canvas is set to a "UI" layer and finally the Canvas is set as a child of the Targeting camera. The canvas contains the targeting reticule right in the middle. Now when the Targeting camera moves the reticule exists in the world space and moves aswell with the Targeting camera, the ship and the targeting reticule all inline at all times.

Currently, if you set the player camera to include the "UI" layer then you'll see the offset reticule on the screen. However the reticule will cut through objects as it exists in world space. To solve this the final step is to create another Camera as a child of the player camera. Set "Clear Flags" to "Depth Only" and "Culling Mask" to "UI" layer. Give it a "depth" higher than the player camera and finally remove the UI layer from player camera's Culling Mask.

The reticule will now be onscreen all the time as expected and will always point to the target correctly.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dustyh55 · Jun 19, 2018 at 10:20 PM 0
Share

This is a really cool idea, it re$$anonymous$$ds me of a red-dot sight for hunting. The reflection of the red dot is always right where the rifle is pointing, even if your head is offset.

avatar image
0

Answer by kiritaku · Nov 12, 2013 at 04:59 AM

well i think there is a fundamental difficulty in aiming accurately from a third person camera. i've seen some game that had a target reticule that would guess the distance to the target and then adjust accordingly... it wasn't great but worked ok. also if your camera is close enough to directly behind the ship and they are exactly aligned, then a cross-hairs in the middle of your camera would work for relatively distant targets, and close ones you would just have to know to aim above them.

so basically the only things i can think of are the automatic distance-adjusting sight, and the fixed on the camera one that you manually compensate for.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Third Person Flight Camera HELP!! 1 Answer

Third person camera aim 0 Answers

Mecanim and body orientation 0 Answers

Problem with mouse look and second skybox cam 0 Answers

How to prevent continuous spinning while aiming at the screen's center? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges