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 cidmodder · Jan 19, 2011 at 01:31 AM · animationjavascriptdoor

on mouse over and at a distance

I currently have a script that opens a door when you press the e button but it opens every door on the level and I just wanted to open the one right in front of me. whats the easiest way to do this? should i do it on mouse over or only at a certain distance or a combination or some other method?

heres the script

function Update () { if (Input.GetKeyDown("e")) {

animation.Play("dooropen"); } }

Thanks!

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

Answer by Flynn · Jan 19, 2011 at 01:36 AM

Unfortunately, for the kind of functionality you want, you need to change how your code works. Your player has to be what opens the door. How can this be done?

First, add the following script to all of your doors.

public function openSaysMe()
{
    animation.Play("dooropen");
}

Name that script Door.

Then, in your player-code, do this.

function Update() { if (Input.getKeyDown("e")) { openClosestDoor(); } }

function openClosestDoor() { var doors : Door[] = FindObjectsOfType(Door) as Door[]; var ldst = Mathf.Infinity; var closest = null; for (var i = 0; i < doors.Length; i++) { var dst = Vector3.Distance(transform.position, doors[i].transform.position); if (dst < ldst) { closest = doors[i]; ldst = dst; } } if (closest != null) { closest.openSaysMe(); } }

Note: Code written from memory. May require tweaking.

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 Jesse Anders · Jan 19, 2011 at 01:43 AM 0
Share

Points to Flynn for a good example. @The OP: Note however that with this code, if you're standing right in front of a door but looking at another door that's in front of you but a little farther away, the door behind you will open, which may not be the desired behavior (which is one of the reasons I suggested raycasting).

avatar image
0

Answer by Jesse Anders · Jan 19, 2011 at 01:36 AM

should i do it on mouse over or only at a certain distance or a combination or some other method?

Yes.

In other words, use whatever method works and gives the desired results. For example, if the door has a collider attached to it, you could use one of the mouse callback functions (as you mentioned), or you could raycast forward from the player position and only open the door during updates when the raycast hits the door in question.

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

No one has followed this question yet.

Related Questions

Canceling animation.Play functions after being used 1 Answer

How to open a door with the "E" key 1 Answer

Start Animation in code is not working 1 Answer

How to set-up a Door that locks behind you after walk in to a room, but need a iPhone as a key to get out? 1 Answer

Raycasting fail 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