Changing Cinemachine Collider size through script
Hello,
I'm making a 2D RPG game, and would like to change the size of my Cinemachine Confiner. I'm envisioning it shrinking over the course of the game to constrain where the player can move, so I tried to set out a simple script to shrink the collider the Confiner is based off of.
The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class Shrinker : MonoBehaviour
{
CompositeCollider2D composite;
BoxCollider2D confiner;
Vector2 confinerSize;
CinemachineConfiner cmConfiner;
// Start is called before the first frame update
void Start()
{
cmConfiner = FindObjectOfType<CinemachineVirtualCamera>().GetComponent<CinemachineConfiner>();
composite = gameObject.GetComponent<CompositeCollider2D>();
confiner = gameObject.GetComponent<BoxCollider2D>();
confinerSize = confiner.size;
Debug.Log(cmConfiner);
Debug.Log(composite);
Debug.Log(confiner);
Debug.Log(confinerSize);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
ShrinkConfiner();
Debug.Log(confinerSize);
}
}
void ShrinkConfiner()
{
cmConfiner.InvalidatePathCache();
confinerSize.x = .9f * confinerSize.x;
confinerSize.y = .9f * confinerSize.y;
}
}
However, when I run this in game, it doesn't seem to shrink the collider at all. The console tells me the collider has gotten smaller, but the Inspector doesn't update the values to be lower. The Confiner also doesn't get any smaller.
Am I doing something obvious wrong? Should I be approaching this a different way? I'm a relative beginner to both C# and Unity, so I'm worried I'm missing something obvious.
Your answer
Follow this Question
Related Questions
How to use External camera on andorid 0 Answers
How to set box collider to a gizmos cube size 0 Answers
Bullet destroy 2 game object intent one Unity 2D 0 Answers
RayCast From Centre of Camera 1 Answer