cubes attracted by a capsule or another cube

Hi everyone,
Here is my first problem:
I am trying to rotate several (5 or less or more) blue cubes (joined together) inside the center of a plane.

The problem occurs when I add a script (drag and drop) to move the blue cubes along the plane.
Indeed the blue cubes seem to be attracted by the left capsule.
see video :

And here is my second question:
I added some code (drag and drop) and this time I use 2 blue cubes and a white cube separately.
the 2 blue cubes move perfectly, but when I click on the white cube to move it,
the 2 blue cubes take the place of the white cube.
Here is the video (maybe it will be more explicit):

Et voilà mon code :

using UnityEngine;

public class dragCube : MonoBehaviour
{
   public GameObject cubeWhite;
   GameObject movingObject;
   Vector3 positionZero;
   Plane plane;

    void Update()
   {
       if (Input.GetMouseButtonDown(0))
       {
           Ray mouseRay = GetMouseRay();
           RaycastHit hit;

           if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit))
           {
               movingObject = hit.transform.gameObject;
               plane = new Plane(Vector3.up, movingObject.transform.position);
               Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
               float rayDistance;
               plane.Raycast(ray, out rayDistance);
               positionZero = movingObject.transform.position - ray.GetPoint(rayDistance);
           }
       }

       else if (Input.GetMouseButton(0) && movingObject)
       {
           Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

           float rayDistance;

           if (plane.Raycast(ray, out rayDistance))
           {
               // c'est ici que je demande au cube blanc de se déplacer
               cubeWhite.transform.position = ray.GetPoint(rayDistance) + positionZero;
           }
       }

       else if (Input.GetMouseButtonUp(0) && movingObject)
       {
           movingObject = null;
       }
   }

   private Ray GetMouseRay()
   {
       Vector3 mousePositionFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.farClipPlane);
       Vector3 mousePositionNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
       Vector3 mousePositionF = Camera.main.ScreenToWorldPoint(mousePositionFar);
       Vector3 mousePositionN = Camera.main.ScreenToWorldPoint(mousePositionNear);

       Ray mouseRay = new Ray(mousePositionN, mousePositionF - mousePositionN);

       return mouseRay;
   }

}

To summarize, the blue cubes seem to be attracted to the capsule and the white cube when you click on one of them, when adding code (here drag and drop).

Thank you for your help,

A+

Simplify each problem case to the fewest number of thing that exhibit the issue.

Now start getting some hard numbers off your GameObject Transform positions.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3