Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Bug Shake camera effect is not visible through the headset

Discussion in 'VR' started by enchev99, Sep 22, 2022.

  1. enchev99

    enchev99

    Joined:
    Nov 22, 2021
    Posts:
    1
    Hello, I have an issue with a camera effect that I am trying to achieve in my game. I am looking to have a camera screen shake which works all fine and even is working in the editor while I have my headset one.
    So the camera view in the editor actually shows the XR Rig camera shaking back and forth but I cannot see this effect appear in the headset. I would be really thankful if someone can help me out with this. Here is the code I have used for the effect:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Shake : MonoBehaviour
    7. {
    8.     public float duration = 1f;
    9.     public float delay = 0f;
    10.     public bool shake = false;
    11.     public AnimationCurve curve;
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if (shake)
    16.         {
    17.             shake = false;
    18.             StartCoroutine(Shaking());
    19.         }
    20.     }
    21.  
    22.     IEnumerator Shaking()
    23.     {
    24.         Vector3 startPos = transform.localPosition;
    25.         float elapsedTime = 0f;
    26.         while(elapsedTime < duration)
    27.         {
    28.             elapsedTime += Time.deltaTime;
    29.             float strength = curve.Evaluate(elapsedTime / duration);
    30.             transform.localPosition = startPos + Random.insideUnitSphere * strength/10;
    31.             yield return null;
    32.         }
    33.         transform.position = startPos;
    34.     }
    35.  
    36.     void startShake()
    37.     {
    38.         shake = true;
    39.     }
    40.  
    41.     public void shakeCam(float time)
    42.     {
    43.         delay = 0f;
    44.         duration = time;
    45.         Invoke("startShake", 0);
    46.     }
    47.  
    48.     public void delayedShakeCam(float delay, float time)
    49.     {
    50.         duration = time;
    51.         this.delay = delay;
    52.         Invoke("startShake", delay);
    53.     }
    54. }
    55.  
     
    Last edited: Sep 22, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    8,597
    Please edit your post to use code-tags when posting code rather than plain text.

    Thanks.
     
    enchev99 likes this.
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,111
    try moving the camera parent transform, i'd guess camera position is overwritten from XR tracking?

    but i wouldn't add camera shake on VR headsets, might be quite uncomfortable..
     
    enchev99 likes this.