Scale object when player is within a given distance of it
What I'm doing with this script is scaling a cube along the y axis (increasing with left mouse button and decreasing with right mouse button) while the player is in close proximity of it (between 2 and .1 units). I want the player to be able to be on top of the cube as it's being scaled. I have the script attached to the cube and the player controller attached to the player variable in the inspector.
It was working like I wanted all day today, but now it's not. Now, the cube is being scaled even when the player isn't near it. I tried starting a new project; it worked briefly and then stopped. Any suggestions as to why this is happening? I don't believe I changed anything, but I'm a total noob, so I could've done something and didn't realize it.
I'm not really looking for any fixes to my script, although any suggestions to make it more efficient would be greatly appreciated. I would just like to figure out where I'm going wrong since, like I said, I had it working earlier.
var player : Transform;
function OnMouseOver ()
{
if(Input.GetButton("Fire1") && (Vector3.Distance(transform.position, player.position) < 2))
{
transform.localScale += new Vector3(0F, .1F, 0);
}
if(Input.GetButton("Fire1") && (Vector3.Distance(transform.position, player.position) > .1))
{
transform.localScale += new Vector3(0F, .1F, 0);
}
if(Input.GetButton("Fire2") && transform.localScale.y > 1f && (Vector3.Distance(transform.position, player.position) < 2))
{
transform.localScale += new Vector3(0F, -.1F, 0);
}
if(Input.GetButton("Fire2") && transform.localScale.y > 1f && (Vector3.Distance(transform.position, player.position) > .1))
{
transform.localScale += new Vector3(0F, -.1F, 0);
}
}
Answer by Oribow · Oct 02, 2015 at 09:38 PM
if(Input.GetButton("Fire1") && (Vector3.Distance(transform.position, player.position) > .1))
{
transform.localScale += new Vector3(0F, .1F, 0);
}
if(Input.GetButton("Fire2") && transform.localScale.y > 1f && (Vector3.Distance(transform.position, player.position) > .1))
{
transform.localScale += new Vector3(0F, -.1F, 0);
}
These two lines doesn't make sense. Why do you allow the player to scale the object, if he is at least 0.1 distance away? I guess you wanted to prevent the player from scaling, if he is to near. But then you gotta connect both resctrictions:
if (Input.GetButton("Fire1") && Vector3.Distance(transform.position, player.position) < 2 && Vector3.Distance(transform.position, player.position) > .1)
{
transform.localScale += new Vector3(0F, .1F, 0);
}
if (Input.GetButton("Fire2") && transform.localScale.y > 1f && Vector3.Distance(transform.position, player.position) < 2 && (Vector3.Distance(transform.position, player.position) > .1))
{
transform.localScale += new Vector3(0F, -.1F, 0);
}
Thanks for the suggestion. Sorry if I didn't make myself clear.
The game object being scaled acts as a sort of elevator that the player needs to be on top of; as it is being scaled, the player ascends and descends with it. The problem I run into is that without a $$anonymous$$imum distance, the game object stops scaling at 2 units. I figured a relatively small unit (like .1) would allow the game object to be scaled past 2 units while the player is on top if it. But now I see, that doesn't really work either way.
What I need is for the player to be able to be x distance (relatively short) from the object to perform the mouse click to make the object begin to scale, but also need the object to be scaled greater than y distance (relatively long).
I would suggest you to make an trigger above your elevator plattform. So the player can only scale the elevator, if he's in the trigger box.