Search Unity

Make a Circle Collider2d's radius update based on a sprite mask's scale and size

Discussion in '2D' started by jpet1991, Aug 12, 2019.

  1. jpet1991

    jpet1991

    Joined:
    Sep 10, 2018
    Posts:
    35
    As the title says, i want to have my player's circle collider radius to adjust as my sprite mask's scale and size gets smaller or larger. I'm running into a problem where i cant convert type Unity.Transform to a float?

    Code (CSharp):
    1.  
    2. public class Player : MonoBehaviour {
    3.  
    4.     SpriteMask sm;
    5.     CircleCollider2D cc;
    6.     bool count;
    7.  
    8.  
    9.     public float MOVEMENT_BASE_SPEED = 1.0f;
    10.  
    11.     [SerializeField] Vector2 movementDirection;
    12.     [SerializeField] float speed;
    13.  
    14.     //[SerializeField] float speedMax; float speedMin = 0.0f;
    15.  
    16.     Rigidbody2D rb;
    17.  
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.         rb = GetComponent<Rigidbody2D>();
    22.         cc = GetComponent<CircleCollider2D>();
    23.         sm = FindObjectOfType<SpriteMask>();
    24.         count = true;
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update () {
    29.  
    30.         Vector2 S = sm.GetComponent<SpriteMask>().transform.localScale;
    31.         gameObject.GetComponent<BoxCollider2D>().size = S;
    32.         gameObject.GetComponent<BoxCollider2D>().size = new Vector2((S.x ),S.y);
    33.  
    34.  
    35.         sm.transform.position = gameObject.transform.position;
    36.  
    37.  
    38.    
    39.  
    40.         sm.transform.localScale -= new Vector3(.1f, .1f, .1f);
    41.            
    42.  
    43.            
    44.            
    45.        
    46.  
    47.        
    48.        
    49.     }
    50.        
    Any suggestions?
     
    Last edited: Aug 12, 2019
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    The code above has the last three lines outside of the "Start" function.