• 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
4
Question by Fr0stbite · Feb 28, 2016 at 04:07 PM · physicsrpgcombat

Physics.Boxcast, how does it work?

I've read the documentation but i don't really understand it. I'm trying to use it for a rpg combat system because the raycast seems to be too thin. Or is there a way to visualize it with a cube?

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

2 Replies

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

Answer by Bunny83 · Feb 28, 2016 at 04:23 PM

Well here's the signature (at least the most important parameters):

 public static bool BoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation = Quaternion.identity)

The parameters are used like this:

  • center: Like any raycast method this is the position from where the box cast starts.

  • halfExtents: Is a vector that defines the size of the cuboid in "local space". So each component represents half the size in that dimension.

  • direction: Is the casting direction just like in all other raycast method.

  • orientation: The orientation defines the rotation of the cube. If you don't pass a value or pass "Quaternion.identity" manually the cube will be aligned with the world axis. You can pass any Quaternion to rotate the cube the way you want.

So for example if you want to cast a cube that is rotated so it's z axis points along your casting direction you can simply do:

 BoxCast(pos, new Vector3(1,2,3), dir, Quaternion.LookRotation(dir));

This will calculate a rotation so the z axis is aligned with your casting direction.

The cube will be 2 units wide (1 half in x), it will be 4 units high (2 half in y) and 6 units long (3 half in z).

Comment
Add comment · Show 2 · 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 Fr0stbite · Feb 28, 2016 at 04:34 PM 1
Share

Thank you! It is better than the documentation :D

avatar image rand_dev · Oct 24, 2017 at 08:53 PM 0
Share

Let say if I have a BoxCollider.size. How would I set the exact BoxCast size so I only want to check the bottom face of the BoxCollider.

avatar image
0

Answer by tanoshimi · Feb 28, 2016 at 04:14 PM

Can't say I've ever used BoxCast - think it must be a very recent addition. However, I have used SphereCast, which provides a "fat raycast" and sounds like it might be more what you're looking for. There's examples on the documentation page.

Comment
Add comment · Show 4 · 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 Fr0stbite · Feb 28, 2016 at 04:22 PM 0
Share

Yeah I' ve tried that but the sphere isn't good because i want to detect the enemy in front of the player and that detects them behind it as well

avatar image tanoshimi Fr0stbite · Feb 28, 2016 at 04:32 PM 0
Share

It shouldn't do - sounds like you're starting the ray from the wrong location.

avatar image Fr0stbite tanoshimi · Feb 28, 2016 at 04:35 PM 0
Share

I started the ray from the position of the player

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

54 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

Related Questions

turn-based and rpg 1 Answer

Melee combat sword system 1 Answer

Rigidbody.velocity Movement code produce wildly different gravity interactions 0 Answers

Ragdoll Wizard, Mesh with fewer joints. 2 Answers

Physics for ball on an incline: rolling up a hill if half way up 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