MatoXD
1
Hello.
I am making a game and I want to have player controller to be offseted, so where I hold down mouse on the screen, from there I can drag my ball. But what happens it that everytime OnMouseButtonDown() its position on X resets to 0, but it would work if that wouldn’t happen. There must be something wrong with offset used or function itself.
private void playerMovementController() {
if ((dieMenuPanel.activeInHierarchy != true) || (resuomePlayingPanel.activeInHierarchy != true)) {
if (Input.GetMouseButton(0)) {
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) {
deltaX = mousePos.x - ballPos.x;
Debug.Log(deltaX.ToString());
Debug.Log("I was here only once");
}
Vector3 BallPos = new Vector3(mousePos.x + deltaX, 2f, this.gameObject.transform.position.z);
gameObject.transform.position = BallPos;
//gameObject.transform.position = Vector3.SmoothDamp(gameObject.transform.position, BallPos, ref velocity, 0.2f);
}
}
}
MatoXD
2
So xxmariofer have solved the problem, for anyone in future, here’s a code, that works
private void playerMovementController() {
if ((dieMenuPanel.activeInHierarchy != true) || (resuomePlayingPanel.activeInHierarchy != true)) {
if (Input.GetMouseButton(0)) {
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) {
deltaX = mousePos.x - gameObject.transform.position.x;
Debug.Log("DeltaX: " + deltaX);
Debug.Log("MousePosX: " + mousePos.x);
}
Vector3 BallPos = new Vector3(mousePos.x - deltaX, 3f, this.gameObject.transform.position.z);
//gameObject.transform.position = BallPos;
gameObject.transform.position = Vector3.SmoothDamp(gameObject.transform.position, BallPos, ref velocity, 0.2f);
}
}
}
Thank you for help