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);
     }
 }

nope, it does not =(

1 Answer

1

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);
        }

Can you make a new project that just has a Resources/data.txt and a script to load it and see if that works? If it doesn't, upload your small project somewhere so we can see it. You could also put the txt file in [StreamingAssets][2] and load it with System.IO.File. [2]: http://docs.unity3d.com/Manual/StreamingAssets.html