Search Unity

Help with Lag Compensation

Discussion in 'Multiplayer' started by owiegand, Dec 20, 2014.

  1. owiegand

    owiegand

    Joined:
    Jan 20, 2014
    Posts:
    17
    Has anyone used the asset Rewinder to implement lag compensation before? If so you might be able to me out. Currently I am having problems getting hit detection to work correctly. Player A can shoot any where and score a hit on Player B but Player B can never hit Player A even when aiming directly at him. Here is the code I am using for lag comp/hit detection.

    Code (CSharp):
    1.  
    2. Ray r = Camera.main.ScreenPointToRay(CenterofScreen);
    3.  
    4. Transform HitBoxThatWasHit;
    5. RewinderHitboxHit hit;
    6.                    
    7. float time = (float)uLink.Network.time;
    8. List<RewinderHitboxHit> hits;
    9. RewinderSnapshot.Raycast(time, r.origin, r.direction, out hits);
    10.            
    11. if (hits.Count > 0) {
    12.        hit = hits.FirstOrDefault();
    13.        RewinderSnapshot.Recycle(hits);
    14.        Debug.Log ("We Hit Something " + hit.Hitbox.Type);
    15.        if (hit.Hitbox.Type == RewinderHitboxType.Proximity)
    16.             return;
    17.        }
    18.                        
    19.        HitBoxThatWasHit = hit.Transform;
    20.                      
    21.       float damagetoSend = 0f;
    22.                        
    23.       uLink.NetworkPlayer PlayerHit = HitBoxThatWasHit.root.gameObject.GetComponent<uLinkNetworkView> ().owner;
    24.       if (!RPCSender.Equals(PlayerHit)) {
    25.           //We Cant Hit Our Selves.
    26.           if (hit.Hitbox.Type == RewinderHitboxType.Head) {
    27.               //HeadShotd
    28.               damagetoSend = 50;
    29.           } else if (hit.Hitbox.Type == RewinderHitboxType.Calf || hit.Hitbox.Type == RewinderHitboxType.Thigh ) {
    30.               //Lower Body
    31.               damagetoSend = 25;
    32.           } else {
    33.               damagetoSend = 25;
    34.           }
    35.                            
    36.           Debug.Log ("Damage Given " + damagetoSend);
    37.           //Send Message To Damage Player
    38.           HitBoxThatWasHit.root.gameObject.GetComponent<PlayerDamage> ().ApplyDamage (damagetoSend);
    39.                            
    40.      } else {
    41.         Debug.LogError ("We Are Not Allowed To Shoot Our Seleves");
    42.       }
    43. } else {
    44.      Debug.Log ("We Didnt Hit Anything");
    45. }  
    46.  
    I have tried a couple of debugging things to see if I could figure out the problem. First, I set the "time" varible in the rewinder raycast to 0. I believe this would effectively make it just like a normal raycast, unfortunately this just produced the same result as before. The second thing I did was to check whether the ray created was always pointing towards the player for some reason thus always resulting in a hit. I created a small cube at the origin and "centerofScreen" vector3. Thees vector3s are set correctly. The only thing I could thing of that might be wrong is how I am using rewinder itself. Any thoughts/tips would be greatly appreciated.