So I have been searching for over an hour, I am getting a headache lol. So I'm working on a super simple 2D game, everything is done basically. However, I don't have an android device or tablet. Therefor, I can't test out the controls properly. In the meantime, I still want to develop the game, so I decided to create a mouse input instead.
The controls are simple, you have a player object, and to replicate touch/swiping, click/drag/release.
- Click and Drag in any direction
- Release adding force in direction of mouse position in relation to PlayerObject position on release.
Kind of like just small pushes in any direction. How can I go about this?
EDIT: I made it apply force, but not how I desire. So with this code its using the center of screen as the base. So if I click in the bottom half of the screen its always forcing it down.
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
if(Input.GetMouseButtonDown(0)){
Vector2 dir = new Vector3(mousePos.x - rb.transform.position.x, mousePos.y - rb.transform.position.x);
Debug.Log("Applying Force in Dir: " dir);
rb.AddForce(dir * multiplier);
}
CodePudding user response:
If I understand you correctly you want to
- MouseDown/TouchBegin on your object
- Drag the mouse/touch in a certain diretcion to define a direction strength
- Release the mouse/touch in order o apply force to the object according to the dragged vector
The objects with attached components
Cube
|--BoxCollider2D
|--Rigidbody2D
|--DragAndReleaseEventHandler2D
|--Example
Visualizer
|--LineRenderer
|--DragAndReleaseVisualizer2D (with reference to Cube's DragAndReleaseEventHandler2D)
Floor
|--BoxCollder2D