- Home /
Drag object with mouse in diagonal direction
I have a gameobject which i want to move using mouse. Currently i can do that in x-axis and y-axis:
public bool moveDiagonal;
public bool moveX, moveY;
public float distanceXmin, distanceXmax, distanceYmin, distanceYmax;
if (moveX)
{
if (curPosition.x >= distanceXmin && curPosition.x <= distanceXmax)
goToMove.transform.position = new Vector3(curPosition.x, goToMove.transform.position.y, 0);
}
else if (moveY)
{
if (curPosition.y >= distanceYmin && curPosition.y <= distanceYmax)
goToMove.transform.position = new Vector3(goToMove.transform.position.x, curPosition.y, 0);
}
I wanted to move my object diagonally as well how to do that according to its direction using mouse?
Answer by s_awali · Nov 27, 2018 at 02:06 PM
You can do it in one single line :
goToMove.transform.position = new Vector3(moveX ? Mathf.Clamp(curPosition.x, distanceXmin, distanceXmax) : goToMove.transform.position.x, moveY ? Mathf.Clamp(curPosition.y, distanceYmin, distanceYmax) : goToMove.transform.position.y, 0);
If you want a more beautiful / not so compact version :
float nextX = goToMove.transform.position.x;
float nextY = goToMove.transform.position.y;
if(moveX)
nextX = Mathf.Clamp(curPosition.x, distanceXmin, distanceXmax);
if(moveY)
nextY = Mathf.Clamp(curPosition.y, distanceYmin, distanceYmax);
goToMove.transform.position = new Vector3(nextX, nextY, 0);
@s_awali this will not constraint the gameobject to be constrained and only move on the line
$$anonymous$$y bad, didn't understood your request. I came up with this yet haven't tested it out. Just let me know ;)
if (moveDiagonal)
{
Vector2 StartVector = new Vector2(distanceX$$anonymous$$, distanceY$$anonymous$$);
Vector2 EndVector = new Vector2(distanceXmax, distanceYmax);
if($$anonymous$$athf.Abs(EndVector.x - StartVector.x) > $$anonymous$$athf.Abs(EndVector.y - StartVector.y))
goTo$$anonymous$$ove.transform.position = Vector3.Lerp(StartVector, EndVector, $$anonymous$$athf.Clamp((StartVector.x - curPosition.x) / (EndVector.x - StartVector.x), 0, 1));
else
goTo$$anonymous$$ove.transform.position = Vector3.Lerp(StartVector, EndVector, $$anonymous$$athf.Clamp((StartVector.y - curPosition.y) / (EndVector.y - StartVector.y), 0, 1));
}
else
{
float nextX = goTo$$anonymous$$ove.transform.position.x;
float nextY = goTo$$anonymous$$ove.transform.position.y;
if (moveX && !moveY)
nextX = $$anonymous$$athf.Clamp(curPosition.x, distanceX$$anonymous$$, distanceXmax);
if (!moveX && moveY)
nextY = $$anonymous$$athf.Clamp(curPosition.y, distanceY$$anonymous$$, distanceYmax);
goTo$$anonymous$$ove.transform.position = new Vector3(nextX, nextY, 0);
}
@s_awali not working properly. But this is my full added , i added yours to:
Your answer
Follow this Question
Related Questions
Click and drag an object if holding a button down. 0 Answers
How to properly convert mouse pos to world pos 1 Answer
OnMouseUp() Click Display Effect. 1 Answer
newbie: drag a rigidbody with mouse 2 Answers
Drag and Drop Using Mouse (2D) 1 Answer