Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Kyle_L · Feb 24, 2015 at 10:55 PM · animationraycastplayerdoor

Opening Door With Raycast

Ok so i have a script in which when the player is so far from the door and presses an Interact key the door they are standing front of opens. Every thing i have seems to be working just fine, except the door wont open without the player being right in front of the door, and i don't know what to do. If someone could help me figure this out that this would that would be awesome.

using UnityEngine; using System.Collections;

public class Door : MonoBehaviour {

 private float distance;
 private GameObject player;
 private Ray ray;

 void Start () {
     player = GameObject.FindGameObjectWithTag ("Player");
 }
 
 void Update () {
 
     distance = Vector3.Distance(transform.position, player.transform.position);
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);

     RaycastHit hit;

     if (Input.GetButtonDown("Interact") && distance < 20 && Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Door") {
         animation.Play("DoorOpening");
     }

 
 }

 }


Comment
Add comment · Show 1
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 jiler · Sep 15, 2018 at 03:16 PM 0
Share

Do you have this script attached to your player GameObject? Also, do you have another script attached to your door GameObject? I am trying to creating this functionality in my project and am having a good deal of trouble. I want to make sure that I have my script placement correct.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bennett_apps · Sep 15, 2018 at 06:29 PM

A better/simpler way to detect door is to put a big sphere trigger on the door, and put something like this on the player:

 void OnTriggerEnter (Collider col) {
     if (col.tag == "Door") {
         col.GetComponent<Animator>().SetBool("OpenDoor", true);
     }
 }

Or similar. Does that make sense?

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 misher · Sep 15, 2018 at 06:40 PM 0
Share

Something like "Interaction $$anonymous$$anager" attached to the same gameobject as player. All interactable objects in scene have trigger colliders of desired form and dimensions, inside interaction manager you check in onTriggerEnter for a custom "interactable" componnet, door could have some component drived from base interactabe class and implement own custom interaction behaviour. Lastly, you can have unified UI working in tandem with your interaction manager

avatar image
0

Answer by karljj1 · Feb 24, 2015 at 10:55 PM

perhaps try a larger value than 20 for your distance check. You can also pass a distance value into your Physics.Raycast instead of performing the check yourself. This is a better solution as your currently checking the distance from the door pivot.

Also make sure nothing is blocking your raycast getting to the door such as another collider.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycasting fail 1 Answer

How to move player after animation 2 Answers

Can I make animations snap to a frame? 1 Answer

Determining rotation for collision avoidance? 3 Answers

Optimizing door script 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