Search Unity

need help with camera shake photon unity

Discussion in 'Scripting' started by irvintiu, Jun 16, 2017.

  1. irvintiu

    irvintiu

    Joined:
    Mar 16, 2014
    Posts:
    135
    i use unity's FPS controller.i manage to make camera shake on the other players.

    the idea is. i shoot, my camera shakes. and when enemy is hit, his camera shakes as well. like feeling the impact.

    so i manage to do this. i use allbuffered. but the problem is, everyone who are not hit are affected as well. i was confused. i manage to do this with damage health script without any problem. (only the target is affected).
    but with the camera shake. it affects everyone. did i miss something or is it something with photonnetwork target?

    i use raycast. and store all my hit components. and able to read parent or child.

    i will show you the script. it may be dirty. was really want to manage how to fix this issue. i have the clean version one before i try to do camera shake on enemies cam.



    my goal for this is, i only want the enemie's cam to shake(who is hit by my raycast) would be the only one affect. not everyone else or yourself.



    Code (CSharp):
    1.  
    2.     public float firerate = 0.1f;
    3.     public float cooldown = 0;
    4.     public float damage = 25f;
    5.     public float impactforce = 50f;
    6.     public float forcemultiplier = 1;
    7.  
    8.     public bool focus = false;
    9.     float offsetvalue;
    10.     void Update()
    11.     {
    12.  
    13.         cooldown -= Time.deltaTime;
    14.  
    15.         if (Input.GetButton("Fire1"))
    16.         {
    17.             Fire();
    18.  
    19.  
    20.  
    21.         }
    22.  
    23.         if (Input.GetButtonDown("Fire2"))
    24.         {
    25.             focus = !focus;
    26.  
    27.         }
    28.  
    29.     }
    30.  
    31.     void Fire()
    32.     {
    33.         if (focus)
    34.         {
    35.             offsetvalue = 0.02f;
    36.         }
    37.         else { offsetvalue = 0.05f; }
    38.         Vector3 offset = new Vector3(Random.Range(-offsetvalue, offsetvalue), Random.Range(-offsetvalue, offsetvalue), 0);
    39.  
    40.         if (cooldown > 0) return;
    41.  
    42.  
    43.         Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward + offset);
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50.         Transform hitTransform;    //name of object you hit
    51.  
    52.         Vector3 hitPoints;    //location of object hit
    53.  
    54.  
    55.         hitTransform = FindCloseshit(ray, out hitPoints);
    56.  
    57.  
    58.  
    59.         if (hitTransform != null)
    60.         {
    61.  
    62.  
    63.             Health h = hitTransform.GetComponent<Health>();   //Health is the script name
    64.  
    65.             while (h == null && hitTransform.parent)
    66.             {
    67.  
    68.                 //use in case if object is in a parent and has the health and collider
    69.  
    70.                 hitTransform = hitTransform.parent;
    71.  
    72.                 h = hitTransform.GetComponent<Health>();
    73.  
    74.  
    75.             }
    76.  
    77.             if (h != null)
    78.             {
    79.  
    80.                 //  h.TakeDamage(damage);
    81.                 PhotonView pv = h.GetComponent<PhotonView>();
    82.  
    83.  
    84.                 if (pv == null)
    85.                 {
    86.                     Debug.LogError("no photonview detected");
    87.                 }
    88.  
    89.  
    90.                 else
    91.                 {
    92.                     h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.AllBuffered, damage);
    93.  
    94.  
    95.                 }
    96.             }
    97.  
    98.             if (hitTransform.childCount > 0)
    99.             {
    100.  
    101.                 var childobject = hitTransform.GetChild(0).GetChild(0);
    102.                 PhotonView pv = childobject.GetComponent<PhotonView>();
    103.                 camerashake CS = childobject.GetComponent<camerashake>();
    104.  
    105.        
    106.  
    107.                 if (pv == null)
    108.                 {
    109.                     Debug.LogError("no photonview detected");
    110.  
    111.                 }
    112.                 else
    113.                 {
    114.                  
    115.                     CS.GetComponent<PhotonView>().RPC("cameraShake", PhotonTargets.AllBuffered);
    116.            
    117.                 }
    118.             }
    119.  
    120.         }
    121.  
    122.         cooldown = firerate;
    123.     }
    124.  
    125.  
    126.  
    127.  
    128.  
    129.  
    130.  
    131.  
    132.     Transform FindCloseshit(Ray ray, out Vector3 hitPoint)
    133.     {
    134.  
    135.         RaycastHit[] hits = Physics.RaycastAll(ray);
    136.  
    137.         Transform closesHit = null;
    138.  
    139.         float distance = 0;
    140.         float maxdistance = 2f;
    141.  
    142.  
    143.         hitPoint = Vector3.zero;
    144.  
    145.  
    146.  
    147.  
    148.         foreach (RaycastHit hit in hits)
    149.         {
    150.             Debug.DrawRay(hit.point, hitPoint);
    151.  
    152.             if (hit.transform != this.transform && (closesHit == null || hit.distance < distance))   //remove maxdistance if its long range
    153.                                                                                                      //&& hit.distance < maxdistance
    154.             {
    155.                 // we haved hit something that is:
    156.                 // 1.) not us
    157.                 // 2.) the first thing we hit(that is not us)
    158.                 // 3.)  or, if not b, is at least closer than the previous closest hit
    159.  
    160.  
    161.  
    162.  
    163.  
    164.                 closesHit = hit.transform;
    165.  
    166.                 distance = hit.distance;
    167.                 hitPoint = hit.point;
    168.  
    169.  
    170.                 Rigidbody pushforce = closesHit.GetComponent<Rigidbody>();
    171.                 if (pushforce != null)
    172.                 {
    173.  
    174.                     pushforce.AddForce(-hit.normal * (impactforce * forcemultiplier));
    175.                 }
    176.  
    177.  
    178.             }
    179.  
    180.         }
    181.  
    182.  
    183.         return closesHit;
    184.     }
     
  2. irvintiu

    irvintiu

    Joined:
    Mar 16, 2014
    Posts:
    135
    code for camerashake


    Code (CSharp):
    1.    public static camerashake instance;
    2.  
    3.    
    4.     Vector3 OrigCampos;
    5.  
    6.     Quaternion OrigCamRo;
    7.  
    8.     Quaternion randomrotate;
    9.  
    10.     public float amplitude = 0.03f;
    11.  
    12.     public float grenadeamp = 0.30f;
    13.  
    14.     public bool connected = false;
    15.  
    16.  
    17.     public float countdown = 10;
    18.  
    19.     public float calculatedcountdown;
    20.  
    21.  
    22.     public float shakeamount =0;
    23.  
    24.     public bool hitgrenade;
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35.     void Start()
    36.     {
    37.         OrigCamRo.z = Camera.main.transform.localRotation.z;
    38.  
    39.         OrigCampos = Camera.main.transform.localPosition;
    40.  
    41.         hitgrenade = false;
    42.  
    43.           instance = this;
    44.  
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.        //OrigCamRo.y = Camera.main.transform.localRotation.y;
    51.  
    52.      //  OrigCamRo.x = Camera.main.transform.localRotation.x;
    53.  
    54.         countdown -= Time.deltaTime;
    55.  
    56.         grenadeshake();
    57.  
    58.  
    59.  
    60.  
    61.  
    62.  
    63.             if (Input.GetKey("m"))
    64.             {
    65.  
    66.  
    67.             Debug.Log("localrotation x is "+Camera.main.transform.localRotation.x);
    68.             Debug.Log("origcamro.x is " + OrigCamRo.x);
    69.  
    70.             Debug.Log("overall local rotations " + Camera.main.transform.localRotation);
    71.  
    72.         }
    73.  
    74.  
    75.  
    76.  
    77.     }
    78.  
    79.  
    80.  
    81.  
    82.     /// <summary>
    83.     /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    84.     /// </summary>
    85.  
    86.     [PunRPC]
    87.     public void cameraShake()
    88.     {  
    89.         Camera.main.transform.localPosition = Camera.main.transform.localPosition + Random.insideUnitSphere * amplitude;
    90.  
    91.     }
    92.  
    93.  
    94.  
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  
    101.  
    102.  
    103.  
    104.     /// <summary>
    105.     /// //////////////////////////////////////////////////////////////////////////////////////////
    106.     /// </summary>
    107.  
    108.     public void restorePos()
    109.  
    110.     {
    111.         Debug.Log("camera in  work");
    112.  
    113.         Camera.main.transform.localPosition = Vector3.Lerp(Camera.main.transform.localPosition, OrigCampos, 1f * Time.deltaTime);
    114.  
    115.         if (Camera.main.transform.localPosition == OrigCampos)
    116.         {
    117.             Debug.Log("camera in localposition!");
    118.         }
    119.     }
    120.    
    121.  
    122.  
    123.            [PunRPC]
    124.     public void grenadehit(float calculatedcountdown)
    125.     {
    126.      
    127.         Debug.Log("RPC WORKS");
    128.         countdown = calculatedcountdown;
    129.      
    130.     }
    131.  
    132.  
    133.  
    134.  
    135.      
    136.  
    137.     void grenadeshake()
    138.  
    139.     {
    140.  
    141.         if (countdown > 0)
    142.        {
    143.  
    144.  
    145.  
    146.           Camera.main.transform.localRotation = Camera.main.transform.localRotation * Quaternion.Euler(0, 0, Random.Range(-100f, 100f));
    147.              }
    148.  
    149.            else if (countdown <= 0)
    150.             {
    151.  
    152.  
    153.        
    154.  
    155.            Camera.main.transform.localRotation = Quaternion.Lerp( Camera.main.transform.localRotation, Quaternion.Euler(Camera.main.transform.localRotation.x, Camera.main.transform.localRotation.y, OrigCamRo.z), 1f * Time.deltaTime);
    156.  
    157.  
    158.         }
    159.  
    160.     }
    161.  
     
  3. ChristianSimon

    ChristianSimon

    Joined:
    Jun 20, 2016
    Posts:
    8
    Hi,

    I guess it is easier to move the discussion to the official forum where you also made this topic. So for anyone who is interest in this, please follow the discussion here.
     
  4. irvintiu

    irvintiu

    Joined:
    Mar 16, 2014
    Posts:
    135
    Code (CSharp):
    1.   public float firerate = 0.1f;
    2.     public float cooldown = 0;
    3.     public float damage = 3f;
    4.     public float impactforce = 50f;
    5.     public float forcemultiplier = 1;
    6.  
    7.     public bool focus = false;
    8.     float recoilvalue;
    9.  
    10.     public float maxrecoilfocus;
    11.     public float maxrecoilnotfocus;
    12.     float offsetvalue;
    13.  
    14.     public static playershoot instance;
    15.     Animator animation;
    16.  
    17.     void Start()
    18.     {
    19.         PhotonView photonView = this.photonView;
    20.  
    21.         animation = GetComponent<Animator>();
    22.  
    23.  
    24.  
    25.         instance = this;
    26.  
    27.         recoilvalue = 0;
    28.  
    29.  
    30.         if (!photonView.isMine)
    31.  
    32.         {
    33.  
    34.  
    35.             GetComponent<playershoot>().enabled = false;
    36.  
    37.  
    38.         }
    39.  
    40.  
    41.  
    42.     }
    43.  
    44.  
    45.  
    46.  
    47.     void Update()
    48.     {
    49.        
    50.  
    51.         cooldown -= Time.deltaTime;
    52.  
    53.         if (Input.GetButton("Fire1"))
    54.         {
    55.             Fire();
    56.        
    57.  
    58.             camerashake.instance.cameraShake();
    59.  
    60.         }
    61.  
    62.         else
    63.         {
    64.             camerashake.instance.restorePos();
    65.  
    66.             recoilvalue = 0;
    67.         }
    68.  
    69.         if (Input.GetButtonDown("Fire2"))
    70.         {
    71.             focus = !focus;
    72.  
    73.         }
    74.  
    75.         if (Input.GetKey(KeyCode.LeftShift))
    76.         {
    77.             focus = false;
    78.  
    79.         }
    80.  
    81.  
    82.  
    83.  
    84.  
    85.  
    86.     }
    87.  
    88.  
    89.  
    90.  
    91.  
    92.     void Fire()
    93.     {
    94.  
    95.  
    96.         if (focus)
    97.         {
    98.  
    99.             recoilvalue += Mathf.Lerp(0f, maxrecoilfocus, 1f * Time.deltaTime);
    100.  
    101.             if (recoilvalue >= maxrecoilfocus)
    102.             {
    103.                 recoilvalue = maxrecoilfocus;
    104.             }
    105.             offsetvalue = 0.02f;
    106.         }
    107.  
    108.         else { offsetvalue = 0.05f;
    109.  
    110.  
    111.             recoilvalue += Mathf.Lerp(0f, maxrecoilnotfocus, 1f * Time.deltaTime);
    112.         }
    113.  
    114.  
    115.  
    116.      
    117.  
    118.         if (recoilvalue >= maxrecoilnotfocus)
    119.         {
    120.             recoilvalue = maxrecoilnotfocus;
    121.         }
    122.  
    123.  
    124.  
    125.         Vector3 recoil = new Vector3(0, recoilvalue, 0);
    126.  
    127.  
    128.         Vector3 offset = new Vector3(Random.Range(-offsetvalue, offsetvalue), Random.Range(-offsetvalue, offsetvalue), 0);
    129.  
    130.  
    131.  
    132.  
    133.         if (cooldown > 0) return;
    134.  
    135.  
    136.         animation.SetTrigger("shoot");
    137.  
    138.         Ray ray = new Ray(Camera.main.transform.position, (Camera.main.transform.forward) + offset + recoil);
    139.  
    140.         Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward);
    141.  
    142.  
    143.  
    144.  
    145.         Transform hitTransform;    //name of object you hit
    146.  
    147.         Vector3 hitPoints;    //location of object hit
    148.  
    149.  
    150.         hitTransform = FindCloseshit(ray, out hitPoints);
    151.  
    152.  
    153.  
    154.         if (hitTransform != null)
    155.         {
    156.  
    157.  
    158.          
    159.  
    160.             Health h = hitTransform.GetComponent<Health>();   //Health is the script name
    161.  
    162.             if (hitTransform.childCount > 0)
    163.  
    164.             {
    165.  
    166.                 var childobject = hitTransform.GetChild(0);
    167.  
    168.                 if (!childobject) return;
    169.  
    170.              
    171.                 camerashake CS = childobject.GetComponent<camerashake>();
    172.      
    173.  
    174.            
    175.  
    176.                 if (CS != null)
    177.  
    178.  
    179.                 {
    180.                     PhotonView pv2 = CS.GetComponent<PhotonView>();
    181.  
    182.                     if (pv2 == null)
    183.                    {
    184.                        Debug.LogError("no photonview detected");
    185.                    }
    186.  
    187.  
    188.                    else
    189.                    {
    190.                        CS.GetComponent<PhotonView>().RPC("cameraShake", PhotonTargets.AllBuffered);
    191.  
    192.                        Debug.Log("working!!!!");
    193.                    }
    194.  
    195.                 }
    196.  
    197.             }
    198.                
    199.      
    200.  
    201.                
    202.  
    203.  
    204.  
    205.          
    206.  
    207.             while (h == null && hitTransform.parent )
    208.             {
    209.  
    210.                 //use in case if object is in a parent and has the health and collider
    211.  
    212.                 hitTransform = hitTransform.parent;
    213.  
    214.                 h = hitTransform.GetComponent<Health>();
    215.  
    216.                
    217.  
    218.  
    219.             }
    220.  
    221.             if (h != null)
    222.             {
    223.  
    224.                 //  h.TakeDamage(damage);
    225.                 PhotonView pv = h.GetComponent<PhotonView>();
    226.  
    227.  
    228.                 if (pv == null)
    229.                 {
    230.                     Debug.LogError("no photonview detected");
    231.                 }
    232.  
    233.  
    234.                 else
    235.                 {
    236.                     h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.AllBuffered, damage);
    237.  
    238.                 }
    239.             }
    240.  
    241.  
    242.          
    243.  
    244.         }
    245.  
    246.         cooldown = firerate;
    247.     }
    248.  
    249.  
    250.  
    251.  
    252.  
    253.  
    254.  
    255.  
    256.     Transform FindCloseshit(Ray ray, out Vector3 hitPoint)
    257.     {
    258.  
    259.         RaycastHit[] hits = Physics.RaycastAll(ray);
    260.  
    261.         Transform closesHit = null;
    262.  
    263.         float distance = 0;
    264.         // float maxdistance = 2f;
    265.  
    266.  
    267.         hitPoint = Vector3.zero;
    268.  
    269.  
    270.  
    271.  
    272.         foreach (RaycastHit hit in hits) // for every hit inside the raycast array
    273.         {
    274.  
    275.  
    276.             //thie transform does not equal to this character
    277.             if (hit.transform != this.transform.parent.parent.parent && (closesHit == null || hit.distance < distance))   //remove maxdistance if its long range
    278.                                                                                                      //&& hit.distance < maxdistance
    279.             {  
    280.                 // we haved hit something that is:
    281.                 // 1.) not us
    282.                 // 2.) the first thing we hit(that is not us)
    283.                 // 3.)  or, if not b, is at least closer than the previous closest hit
    284.  
    285.  
    286.  
    287.  
    288.  
    289.                 closesHit = hit.transform;
    290.  
    291.                 distance = hit.distance;
    292.                 hitPoint = hit.point;
    293.  
    294.                 PhotonView pv = closesHit.GetComponent<PhotonView>();
    295.  
    296.                 Rigidbody pushforce = closesHit.GetComponent<Rigidbody>();
    297.                 if (pushforce != null && pv != null)
    298.                 {
    299.  
    300.                     pushforce.AddForce(-hit.normal * (impactforce * forcemultiplier));
    301.  
    302.                 }
    303.  
    304.  
    305.             }
    306.  
    307.         }
    308.  
    309.  
    310.         return closesHit;
    311.     }