- Home /
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.
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.
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;
}
}
}
}
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
Your answer
Follow this Question
Related Questions
Unity 2D Collider did not create any collision shapes as they all failed verification 0 Answers
Refreshing the Polygon Collider (2D) upon sprite change? 1 Answer
Flipping object with Box Collider while on an angled collider 0 Answers
Platform Collider Not Working When Enemy Collides With It 1 Answer
2D Isometric (2018.3b) - Collider 0 Answers