• 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 /
  • Help Room /
This question was closed Dec 03, 2017 at 08:06 AM by JungOmul for the following reason:

Fixed

avatar image
0
Question by JungOmul · Dec 02, 2017 at 04:53 PM · animationraycastraycastinganimationsdoor

Raycast to play animation

Hi. I'm making door that opens and closes when pressed E. I made Door open and close animations. But it doesn't seems to work.. please help me. I'm desperate. Here is my script. It doesn't make any error.


I fixed my script. I changed the origin of my raycast to my FPSControllerCamera and made my animation to Legacy. It will work just fine if I touch a little in my animations.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Doors : MonoBehaviour {
 
     public Camera fpsCam;
     public AnimationClip Door_Open;
     Animation doorOpen;
     public AnimationClip Door_Close;
     Animation doorClose;
     private bool isOpened = false;
 
 
     void Start()
     {
         isOpened = false;
         doorOpen = GetComponent<Animation> ();
         doorClose = GetComponent<Animation> ();
 
 
     }
 
     void Update ()
     {
         if (Input.GetKeyDown (KeyCode.E)) 
         {
             Ray ray = new Ray (fpsCam.transform.position, fpsCam.transform.forward);
             RaycastHit hit;
             if (Physics.Raycast (ray, out hit, 30.0f)) 
             {
                 if (hit.collider.CompareTag ("Door")) 
                 {
                     if (!isOpened) 
                     {
                         doorOpen.clip = Door_Open;
                         doorOpen.Play ();
                         isOpened = true;
                     }
                 }
 
                 {
                     if (isOpened) 
                     {
                         doorClose.clip = Door_Close;
                         doorClose.Play ();
                         isOpened = false;
                     }
                 }
             }
         }
     }
 }

  
1.png (26.7 kB)
Comment
Add comment · Show 7
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 Hellium · Dec 02, 2017 at 05:00 PM 0
Share

What "does not work"?

I've noticed you never set the value of isOpened to trueor false after playing the animation.

avatar image JungOmul Hellium · Dec 02, 2017 at 05:23 PM 0
Share

I made a animations that make door opens and closes. According to the script, when I press E, the raycast calculate distance of the door and the animation should play and the door opens. But nothing happens.

I set the bool by

if(!isOpened) and if(isOpened)

Do I have to set the bool after pressing E? If so, how should I edit the script?

avatar image JungOmul Hellium · Dec 02, 2017 at 05:36 PM 0
Share

I changed my script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Doors : MonoBehaviour {
 
 
     public AnimationClip Door_Open;
     Animation doorOpen;
     public AnimationClip Door_Close;
     Animation doorClose;
     private bool isOpened = false;
 
 
     void Start()
     {
         isOpened = false;
         doorOpen = GetComponent<Animation> ();
         doorClose = GetComponent<Animation> ();
 
     }
 
     void Update ()
     {
         if (Input.GetKeyDown (KeyCode.E)) {
             Ray ray = new Ray (transform.position, transform.forward);
             RaycastHit hit;
             if (Physics.Raycast (ray, out hit, 30.0f)) {
                 if (hit.collider.CompareTag ("Door")) {
                     if (!isOpened) {
                         doorOpen.clip = Door_Open;
                         doorOpen.Play ();
                         isOpened = true;
                     }
                 }
 
                 {
                     if (isOpened) {
                         doorClose.clip = Door_Close;
                         doorClose.Play ();
                         isOpened = false;
                     }
                 }
             }
         }
     }
 }
                 

But the animation won't play.

avatar image Hellium JungOmul · Dec 02, 2017 at 06:03 PM 0
Share

To debug, add simple Logs inside your if statements and check whether they are printed in your console :

    if (Physics.Raycast (ray, out hit, 30.0f)) {
              Debug.Log("Something hit : " + hit.collider.tag ) ;
              if (hit.collider.CompareTag ("Door")) {
                  Debug.Log( "Door hit " + isOpened ) ;
                  if (!isOpened) {
                      doorOpen.clip = Door_Open;
                      doorOpen.Play ();
                      isOpened = true;
                  }
              }
 
              {
                  if (isOpened) {
                      doorClose.clip = Door_Close;
                      doorClose.Play ();
                      isOpened = false;
                  }
              }
          }
Show more comments

1 Reply

  • Sort: 
avatar image
-1

Answer by LoneWolfSwat · Dec 02, 2017 at 07:06 PM

I dont know very nice about "CompareTag" stuff, but you should just make instaead:

 if(hit.collider.gameObject.tag == "Door") {
  //open it
 }
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 Hellium · Dec 02, 2017 at 07:42 PM 0
Share

No, CompareTag is the efficient way to .... compare the tag of a gameobject. The problem does not come from this function.

avatar image JungOmul · Dec 03, 2017 at 08:09 AM 0
Share

I fixed my script. Changed the origin of raycast to my FPSControllerCamera and set the animations to legacy.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

263 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Door Script Problem 0 Answers

Raycasting Animations 0 Answers

Specific Animation not Playing 0 Answers

Select from multiple character to move 0 Answers

RayCast2D and RayDraw errors 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges