Search Unity

NEED HELP, colliders not working with fast movements

Discussion in 'Physics' started by bo151992, Jun 22, 2016.

  1. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    So I'm working on this project, I'm having issues it is a 3D project, my issue is I have a ball that is going to be moving at high speeds (lets say baseball for example) and when I go to hit it, its moving to fast that it jumps the collider ( goes right through it as if its not there) , it gets skipped in between frames is what I believe, I have tried everything, ive done ton of research, ive tried "dontgothroughthings" ive tried setting up collision with rigidbodys, nothing works, it keeps going through ive read a lot about using a raycast, but I'm not a coder :/ I know a lot with unity, and everything but when it comes to coding I'm completely useless, can someone please help me!
     
  2. PeterMu

    PeterMu

    Joined:
    Oct 1, 2014
    Posts:
    47
    As a "concept" how about:
    1) Raycast in the direction of the velocity and in the opposite direction.
    2) Store the forward collider hit by the raycast each frame.
    3) If your reverse raycast hits the object that the forward raycast hit in the previous frame, assume you went through the collider.
     
  3. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Im a noob with this -_- i know unity but ive never messed with raycast, i have no idea what that means i know the basic physics
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    tbh, doesnt sound like you know unity at all, since coding is pretty much at the heart of it. Go through some learn tutorials.
    No one can help you if you cant help yourself.
     
  5. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Ive used unity alot.... yes im not very well coding smart, ive wrote some small simple codes, and i can put 2 and 2 together and go through scripts to edit and re-assighn things, but idk much when it comes to writing out full blown scripts ive used unity for a while most of the time when i run into issues i look up tutorials and im able to solve them but this isnt that case, i cant find any tutorials on this situtation, and i cant find any classes for C# already looked for those
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Fair enough...

    What velocities are you using?

    What code are you using to put velocity/movement on ball?
     
  7. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Im creating a ping pong style, i only aplied a rigidbody and sphere collider to it, with a box collider, decided not to use a mesh collider because i know they have there own problems the issue im having is sence the ball is moving fast, it goes right through the paddle, caused by the collider being skipped in between the frames the ball passes the collider when moving to the next frame its not responding to the collider, i cant find a way around that, ive seen alot of solutions saying to try the raycaster but ive never personnal used a raycast before never needed it till now apparently
     
  8. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    The box collider is on the paddle*
     
  9. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Here is a video of it being played this is whats happening if u notice the ball (and box when i swing at it), is okay till it gets fast then it just falls through, im trying to get this down and working so i can import it to the actaul project
     
  10. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    What code are you using to move your bat... try to answer the question this time.
     
  11. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Ill post it as soon as get home, im using a stock edited code for steam vr but when i get home ill upload the script
     
  12. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ViveGripExample_Slider : MonoBehaviour {
    5.   private ViveGrip_ControllerHandler controller;
    6.   private float oldX;
    7.   private float MAX_VIBRATION_STRENGTH = 0.2f;
    8.   private float MAX_VIBRATION_DISTANCE = 0.03f;
    9.  
    10.     void Start () {
    11.     oldX = transform.position.x;
    12.   }
    13.  
    14.   void ViveGripGrabStart(ViveGrip_GripPoint gripPoint) {
    15.     controller = gripPoint.controller;
    16.   }
    17.  
    18.   void ViveGripGrabStop() {
    19.     controller = null;
    20.   }
    21.  
    22.     void Update () {
    23.     float newX = transform.position.x;
    24.     if (controller != null) {
    25.       float distance = Mathf.Min(Mathf.Abs(newX - oldX), MAX_VIBRATION_DISTANCE);
    26.       float vibrationStrength = (distance / MAX_VIBRATION_DISTANCE) * MAX_VIBRATION_STRENGTH;
    27.       controller.Vibrate(50, vibrationStrength);
    28.     }
    29.     oldX = newX;
    30.     }
    31. }
    32.  
     
  13. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    that's the code I'm using
     
  14. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Code (CSharp):
    1. using UnityEngine;
    2. using Valve.VR;
    3.  
    4. public class SteamVR_TrackedObject : MonoBehaviour
    5. {
    6.     public enum EIndex
    7.     {
    8.         None = -1,
    9.         Hmd = (int)OpenVR.k_unTrackedDeviceIndex_Hmd,
    10.         Device1,
    11.         Device2,
    12.         Device3,
    13.         Device4,
    14.         Device5,
    15.         Device6,
    16.         Device7,
    17.         Device8,
    18.         Device9,
    19.         Device10,
    20.         Device11,
    21.         Device12,
    22.         Device13,
    23.         Device14,
    24.         Device15
    25.     }
    26.  
    27.     public EIndex index;
    28.     public Transform origin; // if not set, relative to parent
    29.     public bool isValid = false;
    30.  
    31.     private void OnNewPoses(params object[] args)
    32.     {
    33.         if (index == EIndex.None)
    34.             return;
    35.  
    36.         var i = (int)index;
    37.  
    38.         isValid = false;
    39.         var poses = (Valve.VR.TrackedDevicePose_t[])args[0];
    40.         if (poses.Length <= i)
    41.             return;
    42.  
    43.         if (!poses[i].bDeviceIsConnected)
    44.             return;
    45.  
    46.         if (!poses[i].bPoseIsValid)
    47.             return;
    48.  
    49.         isValid = true;
    50.  
    51.         var pose = new SteamVR_Utils.RigidTransform(poses[i].mDeviceToAbsoluteTracking);
    52.  
    53.         if (origin != null)
    54.         {
    55.             pose = new SteamVR_Utils.RigidTransform(origin) * pose;
    56.             pose.pos.x *= origin.localScale.x;
    57.             pose.pos.y *= origin.localScale.y;
    58.             pose.pos.z *= origin.localScale.z;
    59.             transform.position = pose.pos;
    60.             transform.rotation = pose.rot;
    61.         }
    62.         else
    63.         {
    64.             transform.localPosition = pose.pos;
    65.             transform.localRotation = pose.rot;
    66.         }
    67.     }
    68.  
    69.     void OnEnable()
    70.     {
    71.         var render = SteamVR_Render.instance;
    72.         if (render == null)
    73.         {
    74.             enabled = false;
    75.             return;
    76.         }
    77.  
    78.         SteamVR_Utils.Event.Listen("new_poses", OnNewPoses);
    79.     }
    80.  
    81.     void OnDisable()
    82.     {
    83.         SteamVR_Utils.Event.Remove("new_poses", OnNewPoses);
    84.         isValid = false;
    85.     }
    86.  
    87.     public void SetDeviceIndex(int index)
    88.     {
    89.         if (System.Enum.IsDefined(typeof(EIndex), index))
    90.             this.index = (EIndex)index;
    91.     }
    92. }
    93.  
    94.  
     
  15. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    that is the one for tracking the controller but the first script is my script on the actual paddle itself letting me pick up the items and what not
     
  16. PeterMu

    PeterMu

    Joined:
    Oct 1, 2014
    Posts:
    47
  17. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Yes, tried it all with no luck
     
  18. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Is your rigidbody kinematic?

    The major problem youre going to run into is youre using the wrong code to move your paddle, which is why stuff is falling through (well, most likely).

    If the rigidbody is set to kinematic, using transform.postition/rotation is a valid way to move the paddle, if not, you need to use rigidbody.MovePostion/MoveRotation.

    Try that see if it helps with your problem.
     
  19. bo151992

    bo151992

    Joined:
    May 27, 2015
    Posts:
    16
    Yea i set the paddle to kinematic i will try that tho
     
  20. Krishndas

    Krishndas

    Joined:
    Oct 17, 2019
    Posts:
    1
    Does it worked?
     
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Note that the last post you're replying to was in 2016.

    You should never use Transforms on GameObjects to control physics objects. Always use the Rigidbody/Rigidbody2D API to move stuff.
     
  22. Deleted User

    Deleted User

    Guest

    HI!
    Got any more thoughts on this Melv?

    I have been trying for months to achieve pretty much the same thing as this guy, now using unity 2019.
    Just cannot strike it properly and literally every tutorial I can find is either outdated and doesn't work or vaguely mentions raycasting without even enough description for me to look up what they are suggesting.

    I would happily just buy an asset that contained the required scripts, but alas i cant even find that!
     
  23. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    There's nothing to add. The original poster has said they were new to this stuff. There's no fundamental limitation, it's just understanding what you want and using the correct technique.

    The quote from me still stands today as it always has.

    If you have a specific question I can answer I'd also recommend creating a new thread if you have an issue, explaining what you're doing etc. Also note, I'm a 2D physics dev not a 3D one but I do try to help out no matter what.