Choose a specific GameObject in a list

Hello,

If I press Space, I want the player translate to the pnj “statique” if the statique in a range between 1 and 3 from the player.

using UnityEngine;
using System.Collections;

public class deplacement : MonoBehaviour {


	public Transform zag;
	private bool moving = false;
	private Vector3 startPosition;
	private float Weight = 0;
	private float liftSpeed = 3;






	// Use this for initialization
	void Start () {


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


		GameObject[] statiques;
		statiques = GameObject.FindGameObjectsWithTag("Statique");

		foreach (GameObject statique in statiques) {

		float dist = Vector3.Distance(statique.transform.position, transform.position);



		      if (Input.GetKeyDown("space")){

			      startPosition = transform.position;
			      moving = true;

			  
		                                    }

		      if ( (dist < 3) && (dist > 1)  ){

				if(transform.position == statique.transform.position){
					moving = false;
				}
                                                    
		         if(moving ==true) {
		            Weight += Time.deltaTime * liftSpeed; //amount
		            transform.position = Vector3.Lerp(startPosition, statique.transform.position, Weight);
					 
				                   }

				 
		                   


		                              
		                                      }
                 }
	
	}



}

Problem is, the player move but stop when the distance beetween him and the closest statique = 1.

That’s because you have your Translate code inside this conditional:

if ( (dist < 3) && (dist > 1) ){

Once the dist is NOT greater than 1, it won’t run the movement code anymore. Also, you may have more success changing to GetKeyUp so it doesn’t execute that code several times when you intend to only have it execute once per spacebar.

using UnityEngine;
using System.Collections;

public class deplacement : MonoBehaviour {


	public Transform zag;
	private bool moving = false;
	private Vector3 startPosition;
	private float Weight = 0;
	private float liftSpeed = 3;
	private bool distance_move = false;





	// Use this for initialization
	void Start () {


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


		GameObject[] statiques;
		statiques = GameObject.FindGameObjectsWithTag("Statique");

		foreach (GameObject statique in statiques) {

		float dist = Vector3.Distance(statique.transform.position, transform.position);



		      if (Input.GetKeyUp("space")){

			      moving = true;

			  
		                                    }

		      if ( (dist < 3) && (dist > 1)  ){

				 distance_move = true;

			  }

				if(transform.position == statique.transform.position){
					moving = false;
				    distance_move = false;

			 }                                   
		         if( (moving ==true) && (distance_move ==true)) {

				    startPosition = transform.position;
		            Weight += Time.deltaTime * liftSpeed; //amount
		            transform.position = Vector3.Lerp(startPosition, statique.transform.position, Weight);
					 
				                   }

				 
		                   


		                              
		                                      }
                 }
	
	}

My player is in (0,0,0) and I have 3 “statique” in my scene (0,0,2) (0,0,4) and (0,0,6)

when I Press space, the player translate to the static in (0,0,6) :confused: