Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AMIR_REZAs · May 22, 2015 at 04:17 PM · transformquaterniontransform.rotate

How detect player is which side of box?

I have a 3d box like this picture. An a player in 3d. Now how can I detect which side of box my player is? 1 or 2.

alt text

cam00532.jpg (23.6 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · May 22, 2015 at 04:28 PM

You just check the player's coordinates and the box's coordinates:

 if(box.transform.position.z > player.transform.position) {
     // player is on side 1
 }
 else {
     // player is on side 2
 }

If this is supposed to work not matter what the box's rotation is, you'll have to transform the player's position to the box's local space, and check if the player is below the box' position. In the box's local space, the box's position is (0, 0, 0), so in that case, you just check if the player has a negative z:

 if(box.transform.InverseTransformPoint(player.transform.position).z < 0) {
     // player is on side 1
 }
 else {
     // player is on side 2
 }

If you only want to know this while the player is touching the box, put it in your OnTrigger/CollisionEnter.

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 AMIR_REZAs · May 22, 2015 at 04:45 PM 0
Share

Thanks a lot, I'll test it.

avatar image
0

Answer by AMIR_REZAs · May 23, 2015 at 02:49 PM

Here is answer worked from Iron-Warrior

What you're really asking is to test which side of a plane (your box wall) a point (game object position) is on. A plane is defined by a normal (the direction it's pointing) and a location. To check which side of it a point is one, you take the direction from the origin of the plane to the point. If the angle between the direction and your plane's normal is greater than 90 degrees, is on the backside of the plane.

Taking the origin of your plane to be the center of your box...

 Vector3 direction = playerPosition - planeOrigin;
 if (Vector3.Angle(direction, planeNormal) > 90)
 {
 // do something
 }

This is a good way of testing this since it works in all directions and all dimensions.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Adding quaternions using a seperate axis 0 Answers

How to make Y-Axis face away from a position? 2 Answers

Write a C# script to record GameObject rotation? 4 Answers

Drag to Rotate using Arcball theory ( 3 axis ) 0 Answers

How to rotate on one axis while keeping the other axes open to be rotated by other scripts? 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