Hi, I wanted my mouse cursor to snap to a location of an object when a left mouse button is pressed. The code is somewhat working.
Code: I set the mouse position to object location when the button is pressed. Then I continuously track the cursor location and set object position to the location. So the object moves with the cursor.
Problem: After clicking the mouse, I move the cursor with the script and at first it almost goes to the right place. It then moves for 1 or sometimes a few frames where it was when you clicked, and then finally moves to the appropriate location. This makes the model flicker for a split second and drives me crazy.
void Update() {
...
IsLMBHeld();
if (_isLMBHeld) {
Vector3 mousePosition = Mouse.current.position.ReadValue();
mousePosition.z = 2.75f;
Vector3 mousePositionInScene = Camera.main.ScreenToWorldPoint(mousePosition);
print($"_leftArmRigTarget.transform.position: , {_leftArmRigTarget.transform.position}");
print($"mousePositionInScene: , {mousePositionInScene}");
_leftArmRigTarget.transform.position = mousePositionInScene;
}
}
private void IsLMBHeld() {
if (Mouse.current.leftButton.wasPressedThisFrame) {
Vector3 screenPoint = Camera.main.WorldToScreenPoint(_leftArm.transform.position);
Mouse.current.WarpCursorPosition(screenPoint);
_isLMBHeld = true;
}
else if (Mouse.current.leftButton.wasReleasedThisFrame)
_isLMBHeld = false;
}
