• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by el_meida · Jul 09, 2015 at 12:30 PM · colliderif-statementsactive

Trying to make Collider Activate with key code

I am trying to make it so that certain platforms can only be collided with when the mouse is near/on these platforms. When I play the game it plays like it would without the script. Does anyone know a way I can fix the script to do so?

public class PositivePlatform : MonoBehaviour {

 // Use this for initialization
 void Start () {
     gameObject.SetActive (true);
     {
         if (Input.GetKey (KeyCode.Mouse1))
             ;
     }
 }
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 reihe0 · Jul 09, 2015 at 12:33 PM 0
Share

You can't use Input in the Start function. You should use it in void Update()

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Nischo · Jul 09, 2015 at 12:59 PM

This code assumes that its in a script on your platform GameObject that you want to enable when clicked.

 private bool allowCollision;
 void Update()
 {
     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     RaycastHit hit;
     GetComponent<Collider>().enabled = true;
     if (Input.GetMouseButtonUp(0) && Physics.Raycast(ray, out hit)) 
     {
          if (hit.collider.gameObject == gameObject) 
             allowCollision = true;
     }
     GetComponent<Collider>().enabled = allowCollision;
 }

To explain it. We create a ray, that uses the current camera and the mouse screen coordinates. Now to actually allow a raycast to register a hit with our collider, we need to enable it first. If the uses pressed the left mouse button, we make a raycast check against our scene with our created ray. If the found collider is attached to our own gameobject, we allow the collider to stay active, otherwise we disable it directly again.

This is a quick and dirty solution, it would be propably nicer to work with collision layers.

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 el_meida · Jul 09, 2015 at 10:22 PM 0
Share

Hmm. When I plug this in it doesn't seem to be working quite right. It makes the platform not active but does not activate it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

what else could be wrong, OnTriggerEnter 2 Answers

Rotation of the mouse activates a boxcollider2d in javascript. 1 Answer

Possible Unity BUG? Or am i missing something 1 Answer

creating active objects or spots otherwise said HotSpots 1 Answer

My script says object is not colliding with the tag! But it is! 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