[C#]Move object with mouse

Hello,

First I am sorry for my english I am learning but if you understand french i make a post there http://urlz.fr/17P1.

So, I am working on a game project and I need to move object with the mouse, there is not the probleme.
The probleme is when i move an object i can put him trought another like the blue object in this picture.
alt text

Here is the code for moving object.

using UnityEngine;
using System.Collections;

public class PlacementKapla : MonoBehaviour {

   RaycastHit hit;  // contient les info du point de collision du raycast
   GameObject Kapla; // l'objet kapla
   GameObject instantiateKapla; // l'instance de l'objet kapla
   GameObject lastKapla;
   GameObject kaplaSelected;
   GameObject lastKaplaSelected;
   int nbKapla; // le nombre de kapla instancié
   bool isPlaced = false; // pour savoir si l'instance du kapla est placé ou non
   Ray ray; // le rayon

   void Start () 
   {
      Kapla = Resources.Load ("Prefabs/Kapla") as GameObject; // specifie la source de l'objet kapla
      nbKapla = 0; // on commence a 0 kapla
   }


   void Update () 
   {
      ray = camera.ScreenPointToRay (Input.mousePosition);

      placement ();

      //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Probleme quand aucun objet est selected<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

      if(Input.GetMouseButtonDown(0) && Physics.Raycast (ray,out hit) && isPlaced == true)
      {
         if(kaplaSelected != null)
         {
            lastKaplaSelected = kaplaSelected;
         }
         if(lastKaplaSelected != null)
         {
            lastKaplaSelected.GetComponent<KaplaSelected>().selected = false;
         }

         kaplaSelected = hit.transform.gameObject;

         kaplaSelected.GetComponent<KaplaSelected>().selected = true;
      }
   }

   private void placement()
   {
      if(Physics.Raycast (ray,out hit)) // test si il y a une collision ou non
      {
         if(Input.GetKeyDown (KeyCode.Alpha1))
         {
            nbKapla++;
            lastKapla = instantiateKapla;
            if(lastKapla != null)
            {
               lastKapla.layer = LayerMask.NameToLayer ("Default");
            }
            instantiateKapla = (GameObject)Instantiate (Kapla, new Vector3(hit.point.x,Kapla.renderer.bounds.size.y /2 + hit.point.y,hit.point.z),Quaternion.identity); // on instencie le kapla que l'ont met directement dans le GameObject "instantiatekapla"
            instantiateKapla.layer = LayerMask.NameToLayer ("Ignore Raycast"); //pour que l'objet en cours de deplacement ne soit pas influencé par lui-meme
            instantiateKapla.name = "kapla " + nbKapla.ToString ();
            isPlaced = false;
         }
         
         if(!isPlaced)
         {
            instantiateKapla.transform.position = new Vector3(hit.point.x,Kapla.renderer.bounds.size.y /2 + hit.point.y,hit.point.z);
            instantiateKapla.transform.rotation = Quaternion.FromToRotation (instantiateKapla.transform.up,hit.normal) * instantiateKapla.transform.rotation;
            if(Input.GetMouseButtonDown (1))
            {
               instantiateKapla.transform.Rotate (new Vector3(0,90f,0));
            }
         }
         if(Input.GetMouseButtonDown (0))
         {
            isPlaced = true;
            instantiateKapla.layer = LayerMask.NameToLayer ("Default");
            if(lastKapla != null)
            {
               lastKapla.layer = LayerMask.NameToLayer ("Default");
            }
         }
      }
   }
}

Thanks for reading and I hope someone will help me.

Instead of moving the item by setting transform.rotation, you’ll want to move the object through a rigidbody, as that will mean that the physics gets activated. First of all, attach a rigidbody to all of the things you are moving, and ensure that they have colliders.

Then, move the objects by using rigidbody.MovePosition. It works like setting the position, but it takes the physics engine into account, and doesn’t move things through each other.

Now, this depends on what you’re doing, but you probably want the objects to have gravity turned off. If you don’t want to push the things around when you’re not moving them, you’ll have to set the constraints on the rigidbody’s position. Finally, the object you’re moving is probably going to be kinematic, to prevent it from getting pushed around by the things you’re bumping into, but you’ll have to experiment to see what works the best.

Hope that helps!

EDIT: Oh, and a tip; it’s a really bad idea to use non-english variable names. What you end up with is both this really strange mix of languages where your variables mix with built in ones. You also drastically reduce the number of people who can understand what your code is doing - most the people on Unity Answers, for example, won’t be able to help you if they need to understand what your variable names are.