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 jeffreywarf · Sep 02, 2013 at 09:51 PM · vector3distanceangleinteract

Interact with objects based on distance.

I know there seem to be plenty of answers out there for this, but literally none of them work in this case (Especially not that hour long tutorial on youtube that everyone links to, that makes it worse). So I have a script (that I attach to the objects I want to interact with) that is supposed to make it so when the player is within a certain range of the objects they can use them. That part works fine. The objects themselves are of differing sizes, however, so making some way to tell if the player is "facing" them is damn near impossible for me to figure out.

Here is the code as it is now (some of it was following the hour long "opening and closing doors" tutorial on youtube, which doesn't help and gives me wildly inaccurate detection):

 var player : GameObject;
 var opened : boolean;
 var not_opened_color : Color;
 var opened_color : Color;
 var model : GameObject;
 var interact_distance : float = 5.0;
 var can_interact : boolean;
 var PI : float = 22/7;
 var angle : float;
 var current_angle : float;
 
 function Start () {
 
 player = GameObject.FindGameObjectWithTag("MainCamera");
 not_opened_color = Color.green;
 opened_color = Color.red;
 
 }
 
 function Update () {
 
 angle = Vector3.Angle(transform.position - player.transform.position,(transform.position + (player.transform.right * transform.localScale.magnitude)) - player.transform.position);
 current_angle = Vector3.Angle(transform.position,player.transform.forward);
 
 if(Vector3.Distance(transform.position,player.transform.position) <= interact_distance && opened == false)
 {
     if(current_angle < angle)
     {
     can_interact = true;
 
         if(Input.GetKeyUp(KeyCode.E))
         {
         Debug.Log("Interacting");
         opened = true;
         }
     }
     else if(current_angle >= angle)
     {
     can_interact = false;
     Debug.Log("Facing Away");
     }
 }
 else if(Vector3.Distance(transform.position,player.transform.position) > interact_distance || opened == true || current_angle < angle)
 {
 can_interact = false;
 player.GetComponent(InteractorScript).can_interact = false;
 player.GetComponent(InteractorScript).current_object = null;
 }
 
     if(opened == true)
     {
     model.renderer.material.color = opened_color;
     }
     else
     {
     model.renderer.material.color = not_opened_color;
     }
 
 }
 }
 
 function OnGUI () {
 if(can_interact == true)
 {
 GUI.Box(new Rect(Screen.width / 2 - Screen.width / 32, Screen.height / 2 - Screen.height / 64, Screen.width / 16, Screen.height / 32),"[E]Open");
 }
 
 }
Comment
Add comment · Show 12
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 getyour411 · Sep 02, 2013 at 10:04 PM 0
Share

Here's how I deter$$anonymous$$e if I'm facing something (in this case, a bad guy). See if you can adaopt this:

     Vector3 dir = (mobTransform.position - myTransform.position).normalized;
     float direction = Vector3.Dot(dir, myTransform.forward);
     
     
         if(direction > .6f) {

(you can swap $$anonymous$$obTransform for your object) I think I got this from an excellent discussion on Unity Gems.

avatar image jeffreywarf · Sep 02, 2013 at 10:09 PM 0
Share

Looks like some of your code got cut off :P But I'll try it

EDIT: yeah that doesn't work. it works on a 1-axis rotation, but it lets me also interact with the objects whether or not I'm looking too high or too low.

avatar image getyour411 · Sep 02, 2013 at 10:30 PM 0
Share

Do you also want to know if the object is in the Viewport?

avatar image Hyperion · Sep 02, 2013 at 10:32 PM 0
Share

Have you tried using raycasting? That should work. Yeah, I think that's just the thing you're looking for. Here's a link to the scripting reference. It describes just what you want it to do.

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

avatar image jeffreywarf · Sep 02, 2013 at 10:38 PM 0
Share

@getyour411 If the object is in the viewport I don't think that matters, but I managed to get a LOT better accuracy by raising the float value from 0.6f to 0.95f :P now my only problem is it's interacting with multiple objects if they're too close to each other but that's a different problem altogether.

@Hyperion See, I WANTED to use raycast, I just don't understand how it works no matter how many times I read that page.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hyperion · Sep 03, 2013 at 12:44 AM

Okay, since this question needs an answer, here it is. As I said earlier, your plight would probably be best solved by raycasting. Plus you wanted to check the tag. Therefore:

  //You'd need to add an extra variable, like so:
         
         var hitInfo: RaycastHit;
         if(Physics.Raycast(from,to,hitinfo,1)){
         if (hitInfo.collider.tag=="yourTag"){
     //do something
     }
     }
     
 
 

And here's that link: http://answers.unity3d.com/questions/10480/getting-the-objects-name-from-raycasthit.html

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

17 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

Related Questions

Calculate position of camera with set distance and angle. 2 Answers

Find Vector3 from a starting Vector3, angle, and distance. 1 Answer

Distance Variable Won't Change. What's Wrong With My Script? 1 Answer

How to calculate direction between 2 objects 2 Answers

what is wrong with my Vector3.Angle() code? 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