1

I'm developing a mobile game in Unity3d which the player needs to move a stick that is placed just a little bit higher then the finger with transform.position and block a ball that is moved with Force.Mode2D.impulse. The problem is that the ball goes through the stick if the stick is moved too fast. Could anyone please teach me how to code the stick movement with Force (or any other way that works) that still moves according to the finger position on touch screen ( A.K.A Input.mousePosition) instead of using buttons?

The code goes as such if anyone needs the info;

Stick:

 float defencePosX = Mathf.Clamp( Input.mousePosition.x / Screen.width * 5.6f - 2.8f , -2.8f, 2.8f);
 float defencePosY = Mathf.Clamp( Input.mousePosition.y / Screen.height * 10 - 4f, -3.3f, -0.5f);
 this.transform.position = new Vector3 (defencePosX, defencePosY, 0);

Ball:

projectileSpeed = Random.Range (maxSpeed, minSpeed);
 projectileSwing = Random.Range (-0.001f, 0.001f);
 rb.AddForce (new Vector2 (projectileSwing * 1000, 0), ForceMode2D.Impulse);
 rb.AddForce (new Vector2 (0, projectileSpeed), ForceMode2D.Impulse);

a video of the bug: https://youtu.be/cr2LVBlP2O0 basicly if i dont move the stick it hits but if i move it fast the ball goes right through. (the bouncing sound effect doesnt work if itss too fast as well)

Bagumus
  • 55
  • 9
  • i do not want a joystick. basicly the plan is, the stick should come to where ever your finger is on that screen. not to move it with a jistick but with the finger location it self. – Bagumus Sep 17 '17 at 17:15
  • thanks. I will try to upload a in game video so the "stick" becomes claer. – Bagumus Sep 17 '17 at 17:33

2 Answers2

2

When working with physics objects, you'll want to use just the Rigidbody component when moving them. Otherwise, it's interpreted as a teleport and no physics is applied and no movement is calculated.

Try using Rigidbody.MovePosition instead of transform.position.

Also, make sure the Rigidbody components on your stick AND ball both have collisionDetectionMode set to 'Continuous Dynamic'. That's how you get small fast-moving physics objects to hit one another in between frames.

 float defencePosX = Mathf.Clamp( Input.mousePosition.x / Screen.width * 5.6f - 2.8f , -2.8f, 2.8f);
 float defencePosY = Mathf.Clamp( Input.mousePosition.y / Screen.height * 10 - 4f, -3.3f, -0.5f);
 rb.MovePosition(new Vector3 (defencePosX, defencePosY, 0));
BJennings
  • 644
  • 5
  • 9
1

Id recommend that you set the balls force to Vector3.zero before adding force to it, or that you use the collider of your blocking movement as a bounce pad for the ball.

Please remember to check that your colliders are scaled correctly according to the blocker.

A video displaying your issue would be helpful to understand it better.

Doh09
  • 2,324
  • 1
  • 16
  • 31
  • https://youtu.be/cr2LVBlP2O0 Basicly if i dont move the stick it hits but if i move it fast the ball goes right through. (The bouncing sound effect doesnt work if it's too fast as well.) – Bagumus Sep 17 '17 at 17:39
  • If you make the stick move using physics instead of directly altering the transform position, then I believe your problem would be solved. Currently the stick goes through the ball as the stick doesnt move using physics but instead "teleports" to the mouse position. – Doh09 Sep 17 '17 at 21:17
  • i know that that's the problem. I cant think of a way to change my code so it uses force instead of tranform.position any chance you can teach me to do that? – Bagumus Sep 18 '17 at 17:35
  • CnControls, which is a free asset on the Unity Asset store, has a joystick controller in of its example scenes, which provide an input along the horizontal and vertical axises. Now you don't necessarily need a joystick displayed, but the joystick like input might prove useful in applying force in the direction of the players movements, just disable the renderer if you don't want it displayed. It wouldn't be directly on top of where you click, but it would allow for mouse controlled movement. You could then read the input axises and apply a force to a rigidbody2d on the stick. – Doh09 Sep 19 '17 at 11:48
  • https://pastebin.com/0wUyBGiM Here is a code example where I use CnControls. Note that I use FixedUpdate instead of Update, to ensure correct physics and hence force calculations. Adjust the speed variable as necessary. EDIT: I made a typing error, the direction should have been a Vector2, not a Vector3. Also you might wanna rename "Walk" to "Move", I refitted an older script of mine. Remember to lock the rotation of the Rigidbody2D, so only the position changes, and disable gravity if you don't want that applied. – Doh09 Sep 19 '17 at 11:59