Search Unity

touch control and Box Collider2D

Discussion in '2D' started by motivision, Feb 15, 2020.

  1. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    hi everyone


    I have a player on Scene with attached touchcontrol script

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4.  
    5. using System.Collections;
    6. using UnityEngine;
    7.  
    8.  
    9. public class InputTouch : MonoBehaviour
    10. {
    11.  
    12.  
    13.     void FixedUpdate()
    14.     {
    15.  
    16.     #if UNITY_ANDROID
    17.         {      
    18.                
    19.                 if (Input.touchCount > 0 )
    20.                 {
    21.                 transform.Translate(Input.GetTouch(0).deltaPosition * Time.deltaTime * 0.3f);
    22.                 }                  
    23.  
    24.         }
    25.  
    26.     #endif
    27.  
    28.     }
    29.  
    30. }

    honestly I forgot where I find this touch control tutorial

    but thanks this person from youtube for this realization !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


    when I moving player, my player collapse from platforms with Box Collider2D
    that`s always happened when I moving on mobile display UP and Down , platfoms with Box Collider2D ingnore this and collaps in 60% cases


    that`s what I mean for understanding



    player
    Rigidbody 2D
    Box Collider2D

    platforms
    Box Collider2D


    anybody know how to fix this problem


    thanks
     
    Last edited: Feb 15, 2020
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,428
    Do not "move" physics stuff with the Transform. That is what the Rigidbody2D is for so use its API. By changing the Transform you're simply instantly changing the location of a Rigidbody2D/Collider2D and it bypasses the primary reason for adding a Rigidbody2D which is to write poses to the Transform system.

    If you've not added a Rigidbody2D then that's even worse. This means you've added a Collider2D and it's implicitly static (non-moving) yet you're constantly and instantly changing it's position.

    You're also using "Time.deltaTime" rather than "Time.fixedDeltaTime" but you're lucky (and I don't personally like this feature) because during FixedUpdate, Unity returns fixed-time when you read deltaTime because it assumes that what you actually meant.
     
  3. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    big thanks for your reply
     
    MelvMay likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,428
    Np. If I helped then a like is all I ask. ;)
     
  5. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    you helped
    but honestly I can`t understand how to fix this realization :(

    just strong newbie :)
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,428
    If you're just starting with 2D physics then go watch a few starter tutorials like these: https://learn.unity.com/search/?k=["tag:5813f57532b30600250d6e0d","lang:en","q:2d+physics"]

    Don't use Transform to manipulate physics. If something is moving in physics, it's moving because you added a Rigidbody2D. It writes the position and rotation to the Transform for you. You talk directly to the Rigidbody2D by doing things like adding forces, setting velocity or asking it to move to a position or rotation.

    Try something ilke this instead but note I've not tested this and it's not the best way to move stuff:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. using System.Collections;
    6. using UnityEngine;
    7.  
    8. public class InputTouch : MonoBehaviour
    9. {
    10.     public float TouchSpeed = 0.3f;
    11.  
    12.     void FixedUpdate()
    13.     {
    14.          #if UNITY_ANDROID  
    15.          if (Input.touchCount > 0 )
    16.          {
    17.              var rb = GetComponent<Rigidbody2D>();
    18.              var touchOffset = Input.GetTouch(0).deltaPosition * TouchSpeed * Time.fixedDeltaTime;
    19.              var targetPosition = targetPosition.position + touchOffset;
    20.              rb.MovePosition(targetPosition);
    21.           }              
    22.          #endif
    23.     }
    24. }
    Take a look at my profile below, download the GutHub project and load-up the MouseDrag scene. It shows you how to drag stuff using the mouse but can equally be done with any pointer device. It uses a TargetJoint2D and you can set how quickly it follows etc.

    Anyway, It looks like this:
     
    Last edited: Feb 15, 2020
  7. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    you make me happy with your replay
    big thanks I try
     
    MelvMay likes this.
  8. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    I try make adaptation your tips, but that`s really hard for me :(

    Rigidbody2D
    Interpolate Interpolate

    LinearDrag custom gameplay settings

    work for me


    thanks for advices