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 PandawanFr · Apr 11, 2015 at 09:01 AM · c#objectsphysics.raycast

Reset raycast when an object exits

Hello, I have a small problem with my raycast, I want to make a pressure plate, it would basically press when an object is on it, and go back when the object leaves it. But my pressure plate doesn't go back when the object leaves it. I am not using animations for it as it doesn't work with what I did so I'm changing the y axis of the pressure plate to make it go down and back up.

I think it has to do with the else statement that says "else if (p == null) {", because when I try to Debug.Log something inside it, nothing appears... I tried to make the console say what object has been hit by the raycast, but when it exits, it doesn't say null at the end, it just doesn't say anything. The problem is that the recast doesn't reset, so when the objet exits, the raycast kept the object that was found and keeps it until another one is found...

Here is my script if you want it:

It's in C#

 using UnityEngine;
 using System.Collections;
 
 public class PressurePlate : MonoBehaviour {
     
     bool isPressed;
     public float pushAmt;
     public bool nonPlayerAllowed;
 
     public LayerMask layer;
              
     void Update () {
 
         TestForPress ();
     }
 
     void TestForPress () {
 
         Vector3 fwd = transform.TransformDirection(Vector3.up);
 
         RaycastHit hit;
 
         if (Physics.Raycast(transform.position, fwd, out hit, 0.5f, layer)){
             Collider p = hit.collider;    
 
             if (p != null) { 
                 Debug.Log(p);
                 if (nonPlayerAllowed == true) {
                     if (isPressed == true) {
                         Idle ();
                     }else if (isPressed == false) {
                         Press();
                         isPressed = true;
                     }
 
                 }
                 else if (nonPlayerAllowed == false && p.tag == "Player") {
                     if (isPressed == true) {
                         Idle ();
                     }else if (isPressed == false) {
                         Press();
                         isPressed = true;
                     }
                 }
             }
             else if (p == null){
                 if (isPressed == true) {
                     Back ();
                     isPressed = false;
                 }else if (isPressed == false) {
                     Idle ();
                 }
                 
             }
 
         }
     }
 
     void Press () {
         transform.position = new Vector3(transform.position.x,transform.position.y - pushAmt,transform.position.z);
     }
     void Back () {
         transform.position = new Vector3(transform.position.x,transform.position.y + pushAmt,transform.position.z);
     }
     void Idle () {
         transform.position = transform.position;
     }
 
 
 }
 

Thank you!

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
1

Answer by Tehnique · Apr 11, 2015 at 09:06 AM

You are correct in guessing p is never null, and that is because you are inside this if:

  if (Physics.Raycast(transform.position, fwd, out hit, 0.5f, layer)){...}

When the condition for this if is true, p is always != null, just look here. Just change your code to look like:

 if (Physics.Raycast(transform.position, fwd, out hit, 0.5f, layer)
 {...}
 else {
          if (isPressed == true) {
               Back ();
               isPressed = false;
          }else if (isPressed == false) {
                      Idle ();
          }
                  
 }


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 PandawanFr · Apr 11, 2015 at 08:36 PM 0
Share

Thank you it works!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to avoid a DataObject to be null so to be able to be taked by another script? 0 Answers

How to change the color of all objects that use the same material? 0 Answers

Object creation in Update 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