Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
avatar image
0
Question by subdigital · Jun 08, 2016 at 05:27 PM · unity 5physicsbox

Help with 3D Boxcast

I'm pretty new to Unity. I am building an RTS style game for fun and am trying to get drag selection working properly.

I have an orthographic camera looking over the wold at an angle, and I draw the drag start / end using the GUI in screen coordinates. This works.

drag to select

I then figure out the 4 world points for each of the box corners using Camera.main.ScreenToWorldPoint.

 Vector3 wp1 = Camera.main.ScreenToWorldPoint (screenPos1);
 Vector3 wp2 = Camera.main.ScreenToWorldPoint (screenPos2);
 Vector3 wp3 = Camera.main.ScreenToWorldPoint (new Vector2(screenPos1.x, screenPos2.y));
 Vector3 wp4 = Camera.main.ScreenToWorldPoint (new Vector2(screenPos2.x, screenPos1.y));

Using these 4 points I can compute the center, the extents, etc.

 Vector3 wpcenter = Camera.main.ScreenToWorldPoint (
     new Vector2(screenPos1.x + (screenPos2.x - screenPos1.x) / 2f,
             screenPos1.y + (screenPos2.y - screenPos1.y) / 2f)
 );

 Vector3 halfExtents = new Vector3(wpcenter.x - wp1.x, wp1.y - wpcenter.y, 25);

I then do a box cast into the world from the camera to see what I'm selecting:

 Vector3 direction = Camera.main.transform.forward;
 Quaternion rotation = Quaternion.LookRotation (direction);
 RaycastHit[] hits = Physics.BoxCastAll (wpcenter, halfExtents, direction, rotation, Mathf.Infinity);

selection works up to a point

This does sort of work, but I have something wrong and I can't really figure it out. If I drag the selection box really wide or really tall it doesn't select what's under the GUI box.

To help visualize the box cast I create a cube and manually set the vertices to be the 4 world points above and position it in front of the camera. Ideally this would line up perfectly with the GUI selection box, but clearly it does not.

         if (_boxCastVisual == null) {
             _boxCastVisual = GameObject.CreatePrimitive (PrimitiveType.Cube);
             _boxCastVisual.GetComponent<MeshRenderer> ().material.color = Color.yellow;
         }
         _boxCastVisual.transform.transform.position = wpcenter;
          _boxCastVisual.transform.localScale = new Vector3 (halfExtents.x * 2, halfExtents.y * 2, 4);  
         _boxCastVisual.transform.rotation = rotation;

         // move it foward so we can see it
         _boxCastVisual.transform.Translate (Vector3.forward * 100);

The result can be seen in these screenshots:

https://www.dropbox.com/s/we7yufm0uv7mz2m/Screenshot%202016-06-08%2007.54.31.png?dl=0 https://www.dropbox.com/s/sg59pfd1mlb0bhd/Screenshot%202016-06-08%2007.54.36.png?dl=0 https://www.dropbox.com/s/jl8kpph3p3tir55/Screenshot%202016-06-08%2007.54.42.png?dl=0

Can someone point to my error? Thanks!

select1.png (500.5 kB)
select2.png (500.4 kB)
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 GilJoWal · Oct 17, 2020 at 07:44 PM

Hi,

I've been working on this for the past two days and still struggling with few issues. I would check two things in your code: 1) How is your canvas? I switched to Constant Pixel Size (will have weird results Scale with Screen Size) 2) When to apply Local Scale or global scale, it is somethimes confusing with the rotation. Sometimes it's needed to convert global scale point to localscale point.

Good luck!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

WheelCollider - I don't have a circle of Collider 1 Answer

2.5d Player Controller 0 Answers

Non-convex MeshCollider with non-kinematic Rigidbody? 2 Answers

Car physics like cartoon cars 2 Answers

RigidBody immediately stops after AddForce 1 Answer


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