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 /
This question was closed Aug 23, 2017 at 01:37 PM by Nosmo for the following reason:

Other

avatar image
0
Question by Nosmo · Aug 21, 2017 at 05:47 PM · raycastquaternionraycastingraycasthitcomparetag

Door script using Raycast

Im trying to open a door using raycast from a tutorial

here are the scripts ive done so far: interact script: public float interactDistance = 5f;

 void Update ()
 {
     if(Input.GetKeyDown(KeyCode.Mouse0))
         Debug.Log("Mouse button was pressed.");
     {
         Ray ray = new Ray(transform.position, transform.forward);
         RaycastHit hit;

         if(Physics.Raycast(ray, out hit, interactDistance))
         { 
             if(hit.collider.CompareTag("Door")) //transform
             {
                 hit.collider.transform.parent.GetComponent<DoorScript>().ChangeDoorState();
             }
         }
     }
 }

}

Doorscript:

public bool open = false; public float doorOpenAngle = 90f; public float doorCloseAngle = 0f; public float smooth = 2f; //speed door moves

 void Start ()
 {
     
 }
 
 public void ChangeDoorState()
 {
     open = !open;
 }


    void Update ()
 {
     if (open) // same as open == true
     {
         Quaternion targetRotation = Quaternion.Euler(0, 0, doorOpenAngle);
         transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
     }
     else
     {
         Quaternion targetRotation2 = Quaternion.Euler(0, 0, doorCloseAngle);
         transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation2, smooth * Time.deltaTime);
     }
   }

}

when i get to the door it wont open by clicking on left mouse button but it will suddenly swing open if i look at any of the corners of the door.

Can anyone help with this problem?

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 christoph_r · Aug 21, 2017 at 06:58 PM 0
Share

Please make sure to format code properly.

1 Reply

  • Sort: 
avatar image
0

Answer by christoph_r · Aug 21, 2017 at 07:01 PM

Your if conditional in line 3 only executes your logging. The raycasting part is actually executed every frame. For conditionals, it's either:

 if(bool)
   DoStuff();

or:

 if(bool)
 {
   // stuff
 }

Mixing the two doesn't work. Your log call needs to be in the brackets.

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 Nosmo · Aug 22, 2017 at 01:52 PM 0
Share

Thanks for that. When i click on it now it opens and closes but only on the top and bottom of the door.

avatar image christoph_r · Aug 27, 2017 at 09:44 PM 0
Share

What do you mean by "only on the top and bottom"?

Follow this Question

Answers Answers and Comments

98 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

Related Questions

Is this way of making a raycast correct 2 Answers

Raycast returns null for no apparent reason 0 Answers

Raycast not hitting instantiated object when timescale is 0 1 Answer

Rigidbody.position causes shaking 1 Answer

Raycast and RaycastHit failure 2 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