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+