user5488002
user5488002

Reputation:

For statement in Unity issue?

I am trying to "squeeze" a gameObject. When distance starts to be 6.98, execute this code: "The smaller the distance between gameObject1 and gameObject2, the thinner and longer this gameObject is". Somehow, this code has no effect. Why?

public Transform gameObject1;
public Transform gameObject2;

void Update () 
{
     float distance_squeeze = Vector3.Distance(gameObject1.position, gameObject2.position);

     for (distance_squeeze = 6.98f; distance_squeeze > 0; distance_squeeze -= 0.1f) 
     {
          transform.localScale += new Vector3(-0.5F, 0.5F, 0);
     }
}

Upvotes: 1

Views: 50

Answers (1)

Pluto
Pluto

Reputation: 4061

You are calculating the distance between gameObject1 and gameObject2 and then setting it equal to 6.98. Try something like:

 void Update () 
 {
     float distance_squeeze = Vector3.Distance(gameObject1.position, gameObject2.position);    
     if(distance_squeeze < 6.98F){
         transform.localScale = new Vector3(-distance_squeeze, distance_squeeze, 1);
     }   
 }

Upvotes: 1

Related Questions