Search Unity

Make UI or Image following/facing smoothly to camera - like the Unity VR Splashscreen

Discussion in 'AR/VR (XR) Discussion' started by alphakanal, Feb 26, 2020.

  1. alphakanal

    alphakanal

    Joined:
    Jan 21, 2013
    Posts:
    52
    Hi!
    I'm asking myself how to make a UI or Image following smoothly & with some delay the movement of the VR-Camera / Headset - just like the Unity build in Splashscreen does ?

    Thankful for every hint :)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Compute the position out in front of the player, and measure the distance between that and your current billboard position. If it's close enough, leave it alone. If it's farther away, then move it using something like SmoothDamp with generous damping parameters.
     
  3. alphakanal

    alphakanal

    Joined:
    Jan 21, 2013
    Posts:
    52
    Hey, thank you for the reply !! Altough i'm probably one of the worst coders on earth -> i'll try it!! :D
     
  4. alphakanal

    alphakanal

    Joined:
    Jan 21, 2013
    Posts:
    52
    So - got at least a working script -> i attach this script to the object i'd like to follow/face the camera :)
    Code (CSharp):
    1. public Camera Camera2Follow;
    2. public float CameraDistance = 3.0F;
    3. public float smoothTime = 0.3F;
    4. private Vector3 velocity = Vector3.zero;
    5. private Transform target;
    6.  
    7. void Awake()
    8. {
    9.     target = Camera2Follow.transform;
    10. }
    11.  
    12.  
    13. void Update()
    14. {
    15.     // Define my target position in front of the camera ->
    16.     Vector3 targetPosition = target.TransformPoint(new Vector3(0, 0, CameraDistance));
    17.  
    18.     // Smoothly move my object towards that position ->
    19.     transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
    20.  
    21.     // version 1: my object's rotation is always facing to camera with no dampening  ->
    22.     transform.LookAt(transform.position + Camera2Follow.transform.rotation * Vector3.forward, Camera2Follow.transform.rotation * Vector3.up);
    23.  
    24.     // version 2 : my object's rotation isn't finished synchronously with the position smooth.damp ->
    25.     transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, 35 * Time.deltaTime);
    26. }

    While the position dampening works perfect, i'm struggling with a desired "dampening" rotation of my object following the camera's rotation.

    Using the version 1 to rotate my object -> the object is always facing to the camera...works but no "smooth" rotation.
    Using the version 2 to rotate my object -> the rotation is smoothed out but it i'd try to end it synchronously with the position dampening....same when using smoothTime instead of 35 * Time.deltaTime

    hmmm i know why it's not in-synch but i don't know how to "synchronize" the two...any ideas / hints ?
     
    Last edited: Feb 27, 2020
    iangerardmcnamara and zagoh like this.
  5. alphakanal

    alphakanal

    Joined:
    Jan 21, 2013
    Posts:
    52
    Got it!!!
    now i'm using on both: smoothTime * Time.deltaTime :)
     
    JoeStrout likes this.
  6. alphakanal

    alphakanal

    Joined:
    Jan 21, 2013
    Posts:
    52
    Sorry for warming up this thread but i got stuck into a "problem".

    I'll try to keep the object looking at the camera at a fixed heigt ( Y-value) - works fine the update function is as following:
    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.    target.position = Camera2Follow.transform.TransformPoint(new Vector3(0, 0, CameraDistance));
    5.    transform.position = new Vector3(target.position.x, 0.5f, target.position.z);
    6.  
    7. // try to get rotation version 1:
    8. transform.LookAt(transform.position + Camera2Follow.transform.rotation * Vector3.forward, Camera2Follow.transform.rotation * Vector3.up);
    9. // try to get rotation version 2:
    10. transform.rotation = target.rotation;
    11. // doesn't work ->
    12. transform.rotation.y = target.rotation.y;
    13. }
    14.  
    Both versions will rotate all axis x / y / z ... and version 3 doesn't work

    But i'd like to rotate ONLY the y axis and got lost in the Quaternion / Eulers / Vetcor 3 jungle :(

    Thankful for every hint !! :)
     
  7. JeremieB5

    JeremieB5

    Joined:
    Nov 30, 2016
    Posts:
    4
    Hi

    Sorry for necro this thread but this could help.
    Just keep y rotation of your object in your look at target.

    Code (CSharp):
    1. private void Update()
    2. {
    3.  
    4.    
    5.         Vector3 targetPosition = camera.transform.TransformPoint(new Vector3(0, 0, distance));
    6.        
    7.         transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
    8.         var lookAtPos = new Vector3(camera.transform.position.x, transform.position.y, camera.transform.position.z);
    9.         transform.LookAt(lookAtPos);        
    10.  
    11.        
    12.  
    13.    
    14. }