-1

Hello !

I want to make a level editor for a project and for this I need 3D axis to move an object. I have the axes but I want the user to be able to move them by dragging one axis: click on the X axis (red), drag the axis and it moves (like unity, blender or UE does it).

My problem is that I don't know how to get the mouse input to move on the correct axis. Actually, we can move it but when you move the X axis, the mouse doesn't have to follow the X axis in the world but on the screen, so you just have to move your mouse left to right.

Blublub.

Blublub
  • 1
  • 1

1 Answers1

0

Here's one way to do it:

  • Raycast against XZ plane
  • Find the direction vector that starts from the arrows' center and ends at the hit point
  • Since that direction vector can be looking at any direction in XZ plane, project it onto the X axis to constraint it to that axis
  • Move arrows by the resulting vector
Vector3 xAxis = Vector3.right;
Vector3 yAxis = Vector3.up;
Vector3 arrowsCenterPosition = ...;
Plane plane = new Plane( yAxis, arrowsCenterPosition ); // Create a virtual plane on XZ directions
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
if( plane.Raycast( ray, out float enter ) ) // Raycast against that plane
{
    Vector3 hitPoint = ray.GetPoint( enter ); // The point that hit the virtual plane
    Vector3 directionToHitPoint = hitPoint - arrowsCenterPosition;

    // We want to move in X axis but our directionToHitPoint can be in any direction in XZ plane,
    // so we need to project it onto X axis
    Vector3 xAxisMovement = Vector3.Project( directionToHitPoint, xAxis );

    // You can move the arrows by xAxisMovement amount
    arrowsCenterPosition += xAxisMovement;
}
yasirkula
  • 591
  • 2
  • 8
  • 47