• 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 Haamym · Jun 10, 2018 at 12:29 PM · unity 2dcollider2d

Can a Platform effector work on a circle collider2D?

Unity 2D. Can a platform effector work on a circle collider, the reason why I am asking this question is I want a circle collider to let the object coming from outside enter the collider but once it's inside the collider it should not be able to get out of the collider until of course its disable

Have I even tried before asking the question? In my mind, it was pretty simple until I tried it, probably it's easy but I can't wrap my head around it. I thought of disabling it and enabling it once it enters the collider but that will not work if more than one game object is already in the collider other would fall right.

before this becomes an article of me rambling about this, I will make it short and ask, if somebody understand this concept how can I achieve this ? just slap to the right direction would be appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bakir-omarov · Jun 10, 2018 at 01:39 PM

Hey! I think the simplest way is to play with transform of player object (head in GIF). You can lock it relative to the center of circle. Circle2D Custom Effector

i don't know if it is the best way, but you can add this script to any Game Object on the scene, that contain CircleCollider2D checked as a Trigger , and set the tag of your main object to Player.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 // attach this script to any CircleCollider2D object
 [RequireComponent(typeof(CircleCollider2D))]
 public class CircleEffectorCustom : MonoBehaviour {
 
     private bool m_blockingPlayerActivated = false;
     private Transform m_player;
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         // if we triggered with PLayer then set the activasion bool to true
         if (collision.CompareTag("Player") && !m_blockingPlayerActivated)
         {
             m_blockingPlayerActivated = true;
             // set the m_player transform by triggered collision transform (player) , or you can just find it by FindByTag 
             m_player = collision.transform;
         }
             
     }
     void Update()
     {
         // will block only if bool is activated
         if (m_blockingPlayerActivated)
         {
             float radius = GetComponent<CircleCollider2D>().bounds.size.x/2; //radius of circle by its CircleCollider2D, so you can change it runtime by scaling, and script will fit it always
             Vector2 centerPositionOfCircle = transform.localPosition; //center of circle
             float distance = Vector2.Distance(m_player.transform.position, centerPositionOfCircle); //distance from player to circle
 
             if (distance > radius) //If the distance is less than the radius, it is already within the circle.
             {
                 // we will lock its position by some math manupulations
                 Vector2 fromOriginToObject = (Vector2)m_player.transform.position - centerPositionOfCircle;
                 fromOriginToObject *= radius / distance;
                 m_player.transform.position = centerPositionOfCircle + fromOriginToObject;
             }
         }
     }
 }
 







unity-circle2d-lock-effect.gif (180.0 kB)
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
avatar image
0

Answer by Haamym · Jun 11, 2018 at 09:02 AM

Thank you for taking time to help, I will surely try it out in my mind it was so simple that it will work without code, I do have a couple of ideas how to do it with code, thanks

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.

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

87 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

Related Questions

Jumping from Obstacle while adding velocity to Player 0 Answers

Only one collider trigger on same prefab collision 0 Answers

EdgeCollider2D.points returnes Values of wrong gameObject 1 Answer

particle effect repeat twice after collision 0 Answers

Why do GameObjects need Rigidbodies to collide? 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