Search Unity

When i Swipe faster the collision on the object is not getting detected

Discussion in 'Scripting' started by The_Game_Shukla, Jul 5, 2018.

  1. The_Game_Shukla

    The_Game_Shukla

    Joined:
    Jan 26, 2017
    Posts:
    8
    i have a gameobject and i can swipe it left and right but when i swipe faster the collision is not detected i even tried putting continuous Dynamic in each object but it is still going through objects i'll attach my swipe script below and i 'm using Easytouch asset for overall touch inputs and here's the swipe script.





    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using HedgehogTeam.EasyTouch;
    5.  
    6. public class Swipe : MonoBehaviour {
    7.  
    8. //    public GameObject trail;
    9. //    public Text swipeText;
    10.     public Vector3 temp;
    11.     public float swipeforce;
    12.  
    13.     // Subscribe to events
    14.     void OnEnable(){
    15.         EasyTouch.On_SwipeStart += On_SwipeStart;
    16.         EasyTouch.On_Swipe += On_Swipe;
    17.         EasyTouch.On_SwipeEnd += On_SwipeEnd;      
    18.     }
    19.  
    20.     void OnDisable(){
    21.         UnsubscribeEvent();
    22.        
    23.     }
    24.    
    25.     void OnDestroy(){
    26.         UnsubscribeEvent();
    27.     }
    28.    
    29.     void UnsubscribeEvent(){
    30.         EasyTouch.On_SwipeStart -= On_SwipeStart;
    31.         EasyTouch.On_Swipe -= On_Swipe;
    32.         EasyTouch.On_SwipeEnd -= On_SwipeEnd;  
    33.     }
    34.    
    35.  
    36.     // At the swipe beginning
    37.     private void On_SwipeStart( Gesture gesture)
    38.     {
    39.  
    40. //        swipeText.text = "You start a swipe";
    41.         temp = gesture.GetTouchToWorldPoint(5);
    42.     }
    43.    
    44.     // During the swipe
    45.     private void On_Swipe(Gesture gesture)
    46.     {
    47.         //Snake_Controls.Instance.isDied = false;
    48.         // the world coordinate from touch for z=5
    49.         Vector3 position = gesture.GetTouchToWorldPoint(5);
    50.  
    51.         float te = position.x - temp.x;
    52.         Vector3 v3 = transform.position;
    53.     //    float deltaMousePos = Mathf.Abs(te);
    54.         //float sign = Mathf.Sign(te);
    55.  
    56.  
    57.         //BodyParts[0].Translate(Vector3.left * rotationSpeed * Time.deltaTime * deltaMousePos * sign);
    58.         //Snake_Controls.Instance.tail[0].GetComponent<Rigidbody>().AddForce(Vector3.right *-10 * deltaMousePos * -sign);
    59.         v3.x += te*swipeforce/1.2f ;                            //Manage speed here
    60.         v3.x = Mathf.Clamp (v3.x,-17.8f, 17.8f);
    61.  
    62.         //Camera.main.transform.rotation = Quaternion.Euler (new Vector3 (15,0,(v3.x *7f)));
    63.         transform.position = v3;
    64.         temp = position;
    65.  
    66.     }
    67.  
    68.     // At the swipe end
    69.     private void On_SwipeEnd(Gesture gesture)
    70.     {
    71.        
    72.         // Get the swipe angle
    73. //        float angles = gesture.GetSwipeOrDragAngle();
    74.         //swipeText.text = "Last swipe : " + gesture.swipe.ToString() + " /  vector : " + gesture.swipeVector.normalized + " / angle : " + angles.ToString("f2");
    75.     }
    76. }
    77.  

    Any help or Suggestion ?
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    I'm not so familiar with unitys physics engine. But I think the probem is that you set the position of your object every frame directly. This will let you object "teleport" - so even if it looks like a movement, it is not and all colliders in between the previous and the current position are not hit.

    I have three options in my mind:
    1. instead of setting the position of the object you set the "target position" of the object and the object is trying to get there by applying physical forces. This way the physics engine will probably check if there is something in between (if setup correctly). However, the controls might feel a bit unresponsive.
    2. before setting the final object location you let the objects "walk some steps". What I mean is that you position it on the way to the final position multiple times and do a collision check on every step by hand. If you have a good step distance you will catch all collisions on the way (only some which are on the outer border could be missed).
    3. you have another collider which you transform every frame to cover the area between the previous and the current position.

    I think way 3 is the best. It is can be very accurate (if calculated correctly) and is probably the most performant solution.
     
    The_Game_Shukla and FernandoHC like this.
  3. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    338
    I've had some issues like that in the past, especially on low end mobiles, collision would simply skip.
    The solution I found for those cases was to do RayCast on the path made by the object to retroactively check for collision. That helped me on several cases.
    Would be nice if there was a build in solution that could do that already.
     
    The_Game_Shukla likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    @Hosnkobf is correct, a collision won't happen if you teleport the object.

    Solution 3 is really bad, though, and will probably be hard to maintain and cause issues. If you have fast-moving objects, I'd recommend trying two things:

    1: Move the rigidbody in FixedUpdate, using the MovePosition method. Turn on interpolation. The combination of interpolation and FixedUpdate causes the rigidbody to move over subsequent frames. That might be enough to get the collisions to register.

    2: If that's not enough, use the Rigidbody's SweepTest method to check for collisions where you're going.
     
  5. The_Game_Shukla

    The_Game_Shukla

    Joined:
    Jan 26, 2017
    Posts:
    8
    Thanks Guys for your help @Baste @Hosnkobf Problem's been solved ,appreciate your help