- Home /
How to resize a player every couple of seconds?
Hi!
I'm trying to create a function that will decrease the size of my object every couple of seconds while the size is bigger than the x. When the size is smaller than the x, I don't want it to decrease anymore.
I've run the code and there are no errors showing up in the console but my game breaks every time.
Here's my code. I am a beginner so if you can suggest any off-topic changes that could help, I'd be very happy to hear:
//Public fields
public float Increase;
public float Decrease;
public float speed;
public Vector3 left;
public Vector3 right;
public Vector3 forward;
public Vector3 back;
//Private fields
private Rigidbody playerRb;
// Start is called before the first frame update
void Start()
{
playerRb = GetComponent<Rigidbody>();
left = GameObject.Find("PlaneLeft").transform.position;
right = GameObject.Find("PlaneRight").transform.position;
forward = GameObject.Find("PlaneForward").transform.position;
back = GameObject.Find("PlaneBack").transform.position;
}
// Update is called once per frame
void Update()
{
MovePlayer();
KeepPlayerInBounds();
StartCoroutine(DecreaseSize());
}
//Decrease size every couple of seconds while the size is bigger than the x
IEnumerator DecreaseSize()
{
while (transform.localScale.x > 5)
{
yield return new WaitForSeconds(5);
transform.localScale -= new Vector3(Decrease, Decrease, Decrease);
}
}
//Get bigger once player collides with food
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Food")
{
transform.localScale += new Vector3(Increase, Increase, Increase);
Destroy(other.gameObject);
}
}
//Player control movement
void MovePlayer()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * verticalInput * speed * Time.deltaTime / (transform.localScale.x / 4));
transform.Translate(Vector3.right * horizontalInput * speed * Time.deltaTime / (transform.localScale.x / 4));
}
//Keep player in the arena
void KeepPlayerInBounds()
{
float BallBound = transform.localScale.x / 2;
float leftBound = left.x + BallBound;
if (transform.position.x < leftBound)
{
transform.position = new Vector3(leftBound, transform.position.y, transform.position.z);
}
float rightBound = right.x - BallBound;
if (transform.position.x > rightBound)
{
transform.position = new Vector3(rightBound, transform.position.y, transform.position.z);
}
float forwardBound = forward.z - BallBound;
if (transform.position.z > forwardBound)
{
transform.position = new Vector3(transform.position.x, transform.position.y, forwardBound);
}
float backBound = back.z + BallBound;
if (transform.position.z < backBound)
{
transform.position = new Vector3(transform.position.x, transform.position.y, backBound);
}
Answer by Smurfj3 · Mar 15, 2021 at 10:42 AM
Alright well you are currently running the decrease size coroutine every single frame since you are calling it in the Update method. So for instance if you want to run the decrease size method every 5 seconds, you can try something like this:
float timer = 5;
void Update()
{
timer -= Time.deltaTime * 1;
if(timer < 0)
{
//5 seconds are up now lets run the decrease size method and reset the timer
timer = 5;
StartCoroutine(DecreaseSize());
}
}
Secondly, you do this to decrease the size:
transform.localScale -= new Vector3(Decrease, Decrease, Decrease);
But I may be blind but I don't see you initializing Decrease variable anywhere unless you set it in the Inspector. So make sure it has a value. Also make use of Debug.Log("this is a test"); to see if all lines of code are reached. So for example try:
IEnumerator DecreaseSize()
{
while (transform.localScale.x > 5)
{
yield return new WaitForSeconds(5);
transform.localScale -= new Vector3(Decrease, Decrease, Decrease);
Debug.Log("does it decrease or what");
}
}
This should get you started.
Works! But the thing is, the Decrease float keeps increasing every time an action is completed. I set it to 0.25 and then after the first decrease, it sets itself to 0.5, then to 0.75, etc.
Decrease shouldn't change unless you change it somewhere in the code. From your explanation it seems its increasing .25 every "action" and so it should say Decrease += 0.25f; somewhere in your code, just get rid of that.
Answer by bogdankrstic · Mar 16, 2021 at 12:02 AM
Hi! Thank you for your answer. Yes, I assign the values for my public floats in the Inspector. I'll try the timer method and let you know how it goes.