# How to scale player as they travel between 2 points

I'm doing a VR Project where the player walks down a hallway.

The player starts with a size of 10 and is supposed to scale down to a size of 0.5 (well the GameObject in which all the player stuff is inside is supposed to do the scaling). The script below is on the GameObject which has all the player things like the camera, hands and so on are inside it.

I'm not very good at coding so I'm pretty much failing on managing to write this script. this is how far I have gotten:

public class ScalePlayer : MonoBehaviour
{
public GameObject minDistance;
public GameObject maxDistance;

public Vector3 maxScale;
public Vector3 minScale;

Vector3 posStart;
Vector3 posEnd;
Vector3 curDistance;
Vector3 distance;

void Start()
{
posStart = minDistance.transform.position;
posEnd = maxDistance.transform.position;

maxScale = transform.localScale;
}

void Update()
{
distance = posStart - posEnd;

curDistance = transform.position - posEnd;

transform.localScale = Vector3.Lerp(maxScale, minScale, curDistance);
}
}


In my logic I have an invisible object for the start point and one at the end to save where the player is supposed to be big and where they should be small.

I don't know though how or where to put my scaling values (10 -> 0.5) and the script itself is throwing errors (probabaly because I really am not sure how to get the current position of my player compared to the start and end of the hallway).

But what I hope for, is that when the player walks down the Hallway and gets closer to my invisble end point, they smoothly shrink down to the size of 0.5 on their way there, and when they decide to go back, they scale up gradually on the way to the start to 10 again when they reach the beginning.

Working with Unity 2019 and C#.

Halfway solved my problem but I am Stuck with VRTK4. But thank you for the Edit DMGregory.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScalePlayer : MonoBehaviour
{
public GameObject minDistance;
public GameObject maxDistance;

float StartScale = 10;
float EndScale = 0.5f;

Vector3 posStart;
Vector3 posEnd;
float curDistance;
float dist;

public Transform movingPlayer;

// Start is called before the first frame update
void Start()
{

posStart = minDistance.transform.position;
posEnd = maxDistance.transform.position;

}

// Update is called once per frame
void Update()
{

dist = posStart.x - posEnd.x;
curDistance = movingPlayer.position.x - posEnd.x;
float shrinkProgress = Mathf.Clamp(curDistance, 0, dist) / dist;
float scale = EndScale + ((StartScale - EndScale) * shrinkProgress);
transform.localScale = Vector3.one * scale;
//movingPlayer.transform.position = preScalePos;
//Vector3.Lerp(Vector3.one * StartScale,Vector3.one * EndScale, curDistance);
//Vector3 preScalePos = movingPlayer.transform.position;
//transform.position = child.transform.position;

}
}



Was my first time posting anything anywhere, so sorry for not getting it 100% right the first time with how things are supposed to look.

If anyone has tried something like this with VRTK and has any idea how to scale the player, I would appreciate the help.

• "the script itself is throwing errors" if you want help fixing an error, you should always include the text of the error. Not to mention tagging your question with the engine you're using and formatting your code legibly, which I've fixed for you this time. – DMGregory Dec 10 '19 at 16:55
• It's unclear to me in your edited code what specific problem you're having with VRTK that you need help with. – DMGregory Dec 10 '19 at 17:48

One solution could be to scale it over the remaining distance since you start at 10 then running back and forth would auto scale back up also. You would have to adjust to your needs.

using UnityEngine;


public class ScalePlayer : MonoBehaviour {

public GameObject maxDistance;
public Transform movingPlayer;

void Update()
{
//moving transform example to show movement remove this if you have a move already.
transform.Translate(Vector3.forward * 2 * Time.deltaTime);
//distance is the remaining distance before Moveplayers position and the end position.
var dist = Vector3.Distance(movingPlayer.transform.position, maxDistance.transform.position);
//set the scale to be the remaining distance
transform.localScale = new Vector3(dist,dist,dist);
}