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 PizzaReaver · Apr 22, 2015 at 07:23 AM · unity 5physicstriggerbeginner

Physics.CheckSphere always returns true when it shouldn't

Basically the title, I am fairly new to unity, and i wanted to create a simple trigger that moves the dummy of an endtable. When the player goes in, it should ring true, and then the rest the code will work. Problem is, is that it always returns true. Any help on this one would be greatly appreciated.

public class OpenCabinent : MonoBehaviour {

 public GameObject PlayerPtr;
 public SphereCollider SphereCollide;
 public float DrawerOffset;
 public GameObject Cabinet;


 // Use this for initialization
 void Start () 
 {

 }


 // Update is called once per frame
 void Update () 
 {

     if (Physics.CheckSphere(PlayerPtr.transform.position, SphereCollide.radius) == true) 
     {
         Debug.Log(Physics.CheckSphere(PlayerPtr.transform.position, SphereCollide.radius) == true);
         //this will move the cabinent back to its begining position

         if(Input.GetKey(KeyCode.E) == true)
         {
             if(Cabinet.transform.localPosition.z >= 2.148298)
             {
                 Cabinet.transform.Translate(0,DrawerOffset * Time.deltaTime,0); //* Time.deltaTime);
             }
         }

         //this will move tha cabinent out, until it hits a certain number
         if(Input.GetKey(KeyCode.Q) == true)
         {
             if(Cabinet.transform.localPosition.z <= 2.776)
             {
                 Cabinet.transform.Translate(0,-DrawerOffset* Time.deltaTime,0); //* Time.deltaTime);
             }
         }
     }
 }

}

Comment
Add comment · Show 2
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 AlwaysSunny · Apr 22, 2015 at 05:23 AM 1
Share

Try using OverlapSphere(), then looping over the results, to see what you're "accidentally" hitting.

 Collider[] hits = Physics.OverlapSphere(center, radius);
 for (int i=0; i<hits.Length; i++) Debug.Log( hits[i].gameObject.name );

avatar image PizzaReaver · Apr 22, 2015 at 03:35 PM 0
Share

I can see a bit of the problme, that debug statement is never called at all, so could it be my code implementation, or where I put the sphere collide as a trigger?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vicarian · Dec 27, 2016 at 02:51 PM

If you want functionality to be enabled when the player enters a particular space, you could set up a region using a primitive (such as the referenced Sphere Collider), set its collider state to IsTrigger, then listen for OnTriggerEnter on the script. Set a boolean to true on enter, and false on OnTriggerExit. Then you check that bool before checking the Input in Update.

 bool canInteract;
 
 void Update() {
     if (canInteract)
     {
         if(Input.GetKey(KeyCode.E) == true)
          {
              if(Cabinet.transform.localPosition.z >= 2.148298)
              {
                  Cabinet.transform.Translate(0,DrawerOffset * Time.deltaTime,0); //* Time.deltaTime);
              }
          }
          //this will move tha cabinent out, until it hits a certain number
          if(Input.GetKey(KeyCode.Q) == true)
          {
              if(Cabinet.transform.localPosition.z <= 2.776)
              {
                  Cabinet.transform.Translate(0,-DrawerOffset* Time.deltaTime,0); //* Time.deltaTime);
              }
          }
     }
 }
 
 void OnTriggerEnter (Collider other)
 {
     canInteract = true;
 }
 
 void OnTriggerExit (Collider other)
 {
     canInteract = false;
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make a trigger only affect the collided if its parents name is x 1 Answer

Similar movement to a game (look description) 1 Answer

Collison detection not working? 1 Answer

Rigidbody.MovePosition not really smooth 2 Answers

Enemy can't trigger a hit when the player doesn't move 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