Hello! I can not create the code with a gradual increase in the collider. It turned out static.
How do I make a smooth increase in the size of the collider to 2?
Hello! I can not create the code with a gradual increase in the collider. It turned out static.
How do I make a smooth increase in the size of the collider to 2?
Hello,
Can you please tell us a little bit more information? What kind of collider are you hoping to change?
I wrote a small script which will gradually increase the size of a box collider to 2.
To see this in action, make a new script called ‘ColliderGrow’ and paste the following code into that file. Create a new Cube object and attach this script to it.
using UnityEngine;
using System.Collections;
// We require this gameobject to have a box collider
[RequireComponent (typeof(BoxCollider))]
public class ColliderGrow : MonoBehaviour {
// How big the collider will be in the end
public float finalSize = 2;
// How fast we increase the collider size. Higher = faster
public float increaseSpeed = 1;
// Reference to the box collider
private BoxCollider b_collider;
private void Awake() {
// Set reference to box collider
b_collider = GetComponent<BoxCollider> ();
}
private void Update() {
// Create a vector containing the size of the collider
Vector3 b_size = b_collider.size;
// Check if the size is smaller than our final size
// Since this is a box, all axis will be the same so we only need to check one
if (b_size.x < finalSize) {
// The size is smaller than our final size
// Increase the size a little bit
b_size += Vector3.one * increaseSpeed * Time.deltaTime;
// Check if we overshot our final size
if (b_size.x > finalSize) {
// The size is now bigger than the final size
// Set the size to the final size
b_size = new Vector3 (finalSize, finalSize, finalSize);
}
// Update the collider with the new size
b_collider.size = b_size;
}
}
}
It will be similar for other collider shapes. E.g. For a sphere you work with its radius.
Hope this helps!