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 jacksterooney · Jan 01, 2013 at 08:43 PM · javascriptcollision

Check if an object is at a relative position?

Is there a javascript function where a object checks if another object (or tag) is at a certain position relative to itself, and then if so does a certain action?

For example, when a corridor segment is created, it checks if there is a segment in front (say 20m) of it. If there is, it will either turn left or right. The same applies if there is a segment to its left, it will go straight or right, etc.

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
2
Best Answer

Answer by clunk47 · Jan 01, 2013 at 08:46 PM

You could try either Physics.Raycast, or Vector3.Distance.

Comment
Add comment · Show 3 · 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 jacksterooney · Jan 01, 2013 at 08:49 PM 0
Share

Thanks, but I don't think they would be able to differentiate between when somethings to your left or right, they only return the distance. Thanks anyway!

avatar image clunk47 · Jan 01, 2013 at 08:53 PM 1
Share

Of course Raycast would be able to tell you if something is to your left, right, below, above, in front of, or behind you. If you read the script ref I linked you to, you will see in the example that it is checking what is in front of the object by using var fwd = transform.TransformDirection(Vector3.forward)...

avatar image Piflik · Jan 01, 2013 at 08:57 PM 1
Share

You get a Vector that you can compare with the object's transform, via Vector3.Cross and/or Vector3.Dot. I once wrot a script to calculate the angle between two Vectors (including CW or CCW)...

 curAngle = Vector3.Angle((curPos - lastPos).normalized, transform.forward);
 clockwise = angleDir(transform.forward, (curPos - lastPos).normalized, Vector3.up);
 
 function angleDir(fwd: Vector3, targetDir: Vector3, up: Vector3) {
     var perp: Vector3 = Vector3.Cross(fwd, targetDir);
     var dir: float = Vector3.Dot(perp, up);
     
     if (dir > 0.0) {
         return 1.0;
     } else if (dir < 0.0) {
         return -1.0;
     } else {
         return 0.0;
     }
 }
avatar image
0

Answer by Piflik · Jan 01, 2013 at 08:45 PM

Don't think there is an existing script that suits your needs, but you should be able to write one yourself. You could use raycasts or colliders to test if there are obstacles in front of your newly created corridor.

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How to drag a GameObject with touch but at a limited speed 1 Answer

Analysing player metrics help 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