Scaling objects over time

I’m having issues properly scaling object in my game. Specifically, I’m trying to scale my player to a certain amount when he or she collides with certain objects. For example, colliding into a ball will cause the player to scale down to half its normal size; meanwhile, colliding into a square will scale the player to twice its size.

As of right now, I’ve set up a single function to scale the player based on a value other objects set as one of the function’s parameters. When I first made the function, I initially mistook the magnitude to be a single value from the Vector3 I need to create inside the function. Here is the function in full:

//*used in other scripts
	//changes player's scale based on amount used for each player 
	public void AlterPlayerScale(float scaleValue, float initMagnitude, float scalingDuration){
		//set scaling amount for scaling player	
		Vector3 newScale = new Vector3(initMagnitude+scaleValue, 
		                               initMagnitude+scaleValue, 
		                               initMagnitude+scaleValue);
		Debug.Log ("new Scale: "+ newScale);
		
		//scaling step
		scaleStep += Time.deltaTime/scalingDuration;
		Debug.Log ("player's scale: " + transform.localScale.x);
		
		//alter player's scale
		transform.localScale = Vector3.Lerp(transform.localScale, 
		                                           newScale, 
		                                           scaleStep);
	}

I have two objects that alter the player’s scale: a collectable, which should increase the player’s scale; and a enemy, which decreases the player’s scale. Both objects have scripts referencing the function above via OnTriggerEnter() or OnCollisionEnter(), respectively: when the objects collides with the player, the object finds the player’s current local scale magnitude to compare with in the AlterPlayerScale() function above.

However, an issue occurs when the player collides with an enemy after colliding with a collectable: the vector the enemy calculates causes the function to create a positive vector.

I’ve thought of just using one of the three coordinates from a minimum player scale I’ve set up in another script to use for the ‘initMagnitude’ variable instead. However, the vector that comes out as a result will not be relative to the player’s current scale.

As of right now, I still need help scaling the player down properly even after colliding with a collectable. If my description of the problem was confusing, please let me know so I may clarify the problem ASAP.

There are a couple of ways of looking at this problem. You can do as you’ve done and have the object that your player collide with tell the player to grow or shrink based, or you could have the player recognize what it is colliding with and do the right thing. I would lean towards the latter, but since your code does the former, here is a bit of untested code outline how I might approach the problem:

using UnityEngine;
using System.Collections;

public class Scaling : MonoBehaviour { 
	public int startSize = 3;
	public int minSize = 1;
	public int maxSize = 6;
	
	public float speed = 2.0f;
	
	private Vector3 targetScale;
	private Vector3 baseScale;
	private int currScale;
	
	void Start() {
		baseScale = transform.localScale;
		transform.localScale = baseScale * startSize;
		currScale = startSize;
		targetScale = baseScale * startSize;
	}
	
	void Update() {
		transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
		
		// If you don't want an eased scaling, replace the above line with the following line
		//   and change speed to suit:
		// transform.localScale = Vector3.MoveTowards (transform.localScale, targetScale, speed * Time.deltaTime);

		if (Input.GetKeyDown (KeyCode.UpArrow))
			ChangeSize (true);
		if (Input.GetKeyDown (KeyCode.DownjArrow))
			ChangeSize (false);
	}
	
	public void ChangeSize(bool bigger) {
		
		if (bigger)
			currScale++;
		else
			currScale--;
		
		currScale = Mathf.Clamp (currScale, minSize, maxSize+1);
		
		targetScale = baseScale * currScale;
	}    
}

I have a YouTube video about this - YouTube it only requires a few lines of code