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 MrDare360 · Nov 08, 2013 at 07:53 PM · c#animationdistanceelevatoradjacent

Adjacent Elevator is activated.

I have two elevators next to each other running the same script. They are very close to each other and this can't be changed in the level.

The problem is I can open one of the elevators fine but if I'm too close to the adjacent one when doing this or if I'm inside one elevator and press E I open the other one.

This is the layout of my elevators: elevators

The elevator on the right opens when I press E when inside or too far to the right of the elevator on the left but not the other way around. This is the script I'm using:

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class Elevator : MonoBehaviour {
 
     private float maxDistance = 5.5f;
     public Transform elevator;
     public bool open = false;
     public bool verbose = false;
 
     public void Update() {
         GameObject player = GameObject.FindGameObjectWithTag("Player");
         float distance = Vector3.Distance(elevator.transform.position, player.transform.position);
         
         if(verbose) Debug.Log("Distance : " + distance);
 
         if(Input.GetKeyDown("e") && distance < maxDistance && !open) {
             elevator.animation["Default Take"].speed = 1;
             elevator.animation.Play("Default Take");
             open = true;
             if(verbose) Debug.Log("Elevator Opening");
         } else if(open && distance > maxDistance && !elevator.animation.IsPlaying("Default Take")) {
             elevator.animation["Default Take"].speed = -1;
             elevator.animation["Default Take"].time = elevator.animation["Default Take"].length;
             elevator.animation.Play("Default Take");
             open = false;
             if(verbose) Debug.Log("Elevator Closing");
         }
     }
 
     public void Start() {
         
     }
 
 }

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

1 Reply

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

Answer by ThePunisher · Nov 08, 2013 at 08:10 PM

It is quite possible that one of the elevator's scripts is referencing the wrong transform on the public inspector property. Can you verify this is not the case?

Edit: I know why it's able to open the other door. It's all in your if condition. You're condition checks if the the 'E' key has been hit. This condition will pass for both elevators. Then it checks if the distance from the player to the elevator is less than some threshold (5.5 it seems). Obviously, when you are close enough to both elevators both conditions will pass and both elevators would open up. Check out the approach below.

Instead of calculating the distance to the elevator to determine if I should open the door I would use a box collider placed right in front of the elevator door. If you check the isTrigger checkbox then it will call a function named OnTriggerEnter (just like Awake and Start) on every script attached to the GameObject containing this collider. From there you can simply trigger the logic for opening the door. So long as the box colliders don't overlap and the character is not wide enough to inhabit both colliders at any given time, it will only open one elevator.

Here are some more links to help picture what I'm talking about. http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html http://docs.unity3d.com/Documentation/ScriptReference/Collider-isTrigger.html

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 MrDare360 · Nov 08, 2013 at 08:35 PM 0
Share

I can verify that the elevator Transforms are correct.

I have transitioned over to using BoxColliders now, thanks for your help.

avatar image ThePunisher · Nov 08, 2013 at 08:46 PM 0
Share

No worries, let me know how it goes.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Decrease value at half the speed it's increased at? (using Vector3.Distance) 1 Answer

How to get the Quaternion Rotation from a Matrix4x4 2 Answers

Animation disturbing circular rotation 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