Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by MadboyJames · Sep 14, 2019 at 07:01 PM · rtsdotscamera viewport

Real Time Strategy Unit Selection for Perspective Camera

Currently I have this code to select Units using an orthographic camera (following CodeMonkey ECS RTS tutorials on youtube, linked here) (Yes, my project does uses the DOTS framework, but this question should be monobehaviour relevant).

The code:

 Entities.ForEach((Entity entity, ref Translation translation, ref SelectableComponent selectionData) =>
             {
                 float3 entityPosition = translation.Value;
                 if (entityPosition.x >= lowerLeftPosition.x &&
                 entityPosition.z >= lowerLeftPosition.y &&
                 entityPosition.x <= UpperRightPosition.x &&
                 entityPosition.z <= UpperRightPosition.y)
                 {
                     PostUpdateCommands.AddComponent(entity, new SelectedComponent());    
                 }        
             });

This just creates a box where the camera near clipping plane starts and checks to see which entities (objects) are within the box. My question is how do I get the "ground" plane on a perspective camera, because the near clipping plane is so geometrically small? I could raycast to the ground to get the depth, but I wonder if there is a better way to go about it.

PS: I do not expect nor require DOTS syntax, so please answer in classic monobehavour if it suites you.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Envilon · Dec 01, 2020 at 01:02 PM

Hey @MadboyJames , I've done this a while ago, I hope it is still relevant for you. I also followed the tutorial you've referred to with the exception that I used a perspective camera in a 2D game (same as you I suppose). If I understand correctly, the unit selection and so on works the same, the only problem is getting the world space point in your scene from the position of your camera and your mouse position.

I've done this transformation in my input system, which is taking input from the player and filling the data into corresponding components. I also used the new Unity input system. So basically:

I got my mouse position on the screen:

 void UserInputs.IGameControlsActions.OnMousePosition(InputAction.CallbackContext context) =>
             _mousePosition = context.ReadValue<Vector2>();

And I've converted it to the world space point on XY plane:

 private float3 GetCursorPositionOnXYPlane() {
             var mouse = new float3(_mousePosition.x, _mousePosition.y, -Camera.main.transform.position.z);
             var worldSpacePoint = Camera.main.ScreenToWorldPoint(mouse);
 
             return new float3(worldSpacePoint.x, worldSpacePoint.y, 0);
         }

The minus sign before the camera's Z position because mine is always positioned "on the negative side from XY plane".

Then I just pass this position to some components for my selection system or cursor movement system or whatever.

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

183 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 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 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 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 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 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 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 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 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 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

How to selected objects drawing a box like in rts games 1 Answer

Selecting one instantiate prefab 1 Answer

Warning messages lagging game 0 Answers

Setting a variable using ViewportToWorldPoint 1 Answer

I can a render all these huge objects? 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