Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Kek_Kek · Apr 21, 2018 at 12:09 AM · animationraycasting

Scripting Help

I made a script that uses Raycasting. When the players raycast hits a door or a drawer tagged "Open" the player is able to press 'E' and an opening or closing animation would play. The problem is that this works for only one door/drawer. Is their anyway to fix this?

  {
     public float distance;
     public GameObject raycastObject;
     public GameObject OpenPanel1;
     public GameObject OpenPanel2;
     private bool Seen;
     public Animator animator;
 
 
     // Use this for initialization
     void Start () {
         OpenPanel1.SetActive(false);
         OpenPanel2.SetActive(false);
 
     }
     private bool IsOpenPanel1Active
     {
         get
         {
             return OpenPanel1.activeInHierarchy;
         }
     }
     private bool IsOpenPanel2Active
     {
         get
         {
             return OpenPanel2.activeInHierarchy;
         }
     }
 
     // Update is called once per frame
     void Update () {
         RaycastHit hit;
 
         Vector3 fwd = raycastObject.transform.TransformDirection(Vector3.forward);
 
         Debug.DrawRay(raycastObject.transform.position, fwd * 50, Color.green);
 
         if (Physics.Raycast(raycastObject.transform.position, fwd, out hit, distance))
         {
 
             if (hit.collider.gameObject.tag == "Key")
             {
                 Debug.Log("Visible");
              
 
             }
             if (hit.collider.gameObject.tag == "Open")
             {
                 Debug.Log("Door");
                 OpenPanel1.SetActive(true);
                 Seen = true;
             }
         }
 
         if(IsOpenPanel1Active && Seen == true)
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 OpenPanel1.SetActive(false);
                 animator.SetBool("open", true);
                 OpenPanel2.SetActive(true);
             }
         }
         if(IsOpenPanel2Active && Seen == true)
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 OpenPanel2.SetActive(false);
                 animator.SetBool("open", false);
                 OpenPanel1.SetActive(true);
             }
         }
     }
 }
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 oStaiko · Apr 21, 2018 at 12:39 AM

Few bits of advice first: Dont store your raycast as a GameObject, but as a Transform. You can just write "public Transform raycastTransform" and instead of writing "raycastGameObject.transform" 4 times, you can just write "raycastTransform". Second, its never recommended to use tags, I'm pretty sure they only exist for prototyping/debugging, but the ideal case is to have scripts on the object you're tagging that you can detect with GetComponent(). If you stick with tags, you wont be able to have an object that is both a key and a door. It may sound weird now, but if you want some objects to be burnable so to say, you wont be able to have doors on fire. Third, c#, you don't need to write (Seen == true), you can just write (Seen) and (!Seen). It is also good practice to follow common naming conventions, where variables start with lowercase, and methods start in uppercase.


As for problems in the logic of your code, starting on line 56, when the if statement passes, you enable panel 1. Skip to line 65, and now no matter what panel 2 was before this, 65 will pass and then disable panel 2, and enable panel 1, though we just disabled it. More importantly, it also sets the animation back to off. So in short, whenever you start the animation, you turn it off the very same frame. To make it work you need else and else if statements.


To fix:

 {
     [Header("References")]
     public Transform raycastTransform;
     public GameObject openPanel1;
     public GameObject openPanel2;
     public Animator animator;
     [Header("Values")]
     public float distance;
     private bool seen;
 
     private bool isOpenPanel1Active
     {
         get
         {
             return openPanel1.activeInHierarchy;
         }
     }
 
     private bool isOpenPanel2Active
     {
         get
         {
             return openPanel2.activeInHierarchy;
         }
     }
         
     void Start ()
     {
         openPanel1.SetActive(false);
         openPanel2.SetActive(false);
     }
 
     void Update ()
     {
         RaycastHit hit;
         Vector3 fwd = raycastTransform.TransformDirection(Vector3.forward);
         Debug.DrawRay(raycastTransform.position, fwd*distance, Color.green);
 
         if (Physics.Raycast(raycastTransform.position, fwd, out hit, distance))
         {
             // KeyScript ks = hit.collider.GetComponent<KeyScript>();
             // DoorScript ds = hit.collider.GetComponent<DoorScript>();
             if (hit.collider.gameObject.tag=="Key") // if (ks) {}
             {
                 Debug.Log("Visible");
             }
             else if (hit.collider.gameObject.tag=="Open") // else if (ds) {}
             {
                 Debug.Log("Door");
                 openPanel1.SetActive(true);
                 seen = true;
             }
             else
                 seen = false;
         }
         else
             seen = false;
 
         if (Input.GetKeyDown(KeyCode.E) && seen)
         {
             if (isOpenPanel1Active)
             {
                 openPanel1.SetActive(false);
                 animator.SetBool("open", true);
                 openPanel2.SetActive(true);
             }
             else if (isOpenPanel2Active)
             {
                 openPanel2.SetActive(false);
                 animator.SetBool("open", false);
                 openPanel1.SetActive(true);
             }
         }
     }
 }

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

215 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

Related Questions

How to fix click range & time delay on animated object 0 Answers

Playing animation for object if hit by player camera s ray 1 Answer

Raycast only works right next(practically on top of) object 0 Answers

GUI Button vs RayCasting as input for random animation. 0 Answers

animation problem 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