0
\$\begingroup\$

I have a cube and it's on the ground and there are two walls (Plane) next to the ground. enter image description here

Like in the image there is a ground and in side there is a wall. Here is my code:

    void Update() {

      transform.Translate(userDirection * speed * 14f * Time.deltaTime); 
    transform.Translate(mSpeed * Input.GetAxis("Horizontal") * 2 *  Time.deltaTime, 0f, mSpeed * Input.GetAxis("Vertical") * 2 *  Time.deltaTime);


    if (Input.touchCount == 0)
        return;
    Touch touch = Input.GetTouch(0);
    float originalY = transform.position.y;
    float cameraZ = Camera.main.WorldToScreenPoint(transform.position).z;

    if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Began) {
        Vector3 screenPoint = new Vector3(touch.position.x, touch.position.y, cameraZ);
        Vector3 worldPoint = Camera.main.ScreenToWorldPoint(screenPoint);
        worldPoint.y = originalY;
        transform.position = worldPoint;
    }
}

My cube goes through the side walls, but on laptop it works perfectly, and on android device my cube goes through the side walls. Can anyone please tell why is my cube is going through side walls? Any help is appreciated!

\$\endgroup\$
  • \$\begingroup\$ You shouldn't use Transform.translate to move the cube. Try to attach a rigidbody to the cube and move the cube using physics. \$\endgroup\$ – user100681 Nov 11 '17 at 16:09
  • \$\begingroup\$ @GabrieleVierti I do have rigidbody attached to my cube \$\endgroup\$ – Hemlata Nov 11 '17 at 16:19
  • \$\begingroup\$ You see, now you are moving the cube against the walls, using Transform.translate . The problem is that both the cube and the wall collider don't want to pierce each other, so they try to apply forces on each other in order to stay separated. The cube to is STILL trying to go through the wall, and by doing this constantly, every frame, it is going to go through the wall. using a rigidbody things change: the rigidbody tries to go through the wall BUT, it encounters the collider. At this point the rigidbody stops going, and doesn't try to go through the wall, like Transform.translate does. \$\endgroup\$ – user100681 Nov 11 '17 at 16:27
  • \$\begingroup\$ You do have a rigidbody on the cube, but you are not using it in your script. Right now it is just applying gravity on the cube object, but it is not moving it with your input. \$\endgroup\$ – user100681 Nov 11 '17 at 16:29
  • \$\begingroup\$ Can you please tell me how to do that? \$\endgroup\$ – Hemlata Nov 11 '17 at 16:29
2
\$\begingroup\$

Here is a simple example on how to make the movement a lot smoother and more natural(this also solves the problem of the cube passing through the walls): https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial/moving-player

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Your cube is probably going through walls because your Update() method doesn't get called as often as it does on a laptop. The android device will most likely have something near 30 FPS and that means 30 calls to the update method. If your cube can move behind the plane in that time, then it will be counted as behind the plane.

You could use a 3D object like a cube instead of a plane to offset this. You could also just change the plane's collider to a cube collider and make it thicker so that your cube can't go through it. See badly drawn example below:

enter image description here

Of course, using built in physics will handle most of the work instead if you choose to go that route. It will also use the FixedUpdate() method and will behave the same way in all systems (or at least try to do so).

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.