Search Unity

Unity VR style camera movement.

Discussion in 'Scripting' started by DreamingInsanity, Sep 20, 2019.

  1. DreamingInsanity

    DreamingInsanity

    Joined:
    Nov 5, 2017
    Posts:
    101
    This is the best way I thought to be able to describe what I want to do. It is pretty much the camera movements you get from looking around in a VR headset - like you are "in the world". The basic concept is that the camera rotates to follow the mouse, up until a certain point where it stops.
    I have tried two ways, one using positions:
    Code (CSharp):
    1. public Vector3 mousePos;
    2.     public Transform origin;
    3.     public float clampDist;
    4.     Vector3 velocity = Vector3.up;
    5.     public float smoothTime = 0.3F;
    6.  
    7.     void Update()
    8.     {
    9.         var mouseCentered = Input.mousePosition;
    10.         mouseCentered.x -= Screen.width/2;
    11.         mouseCentered.y -= Screen.height/2;
    12.  
    13.         mousePos = new Vector3(mouseCentered.x / 10, mouseCentered.y / 10, 0);
    14.         Vector3 pos = new Vector3(origin.position.x, origin.position.y, 0) + mousePos;
    15.  
    16.         transform.position = Vector3.SmoothDamp(transform.position, pos, ref velocity, smoothTime);
    17.  
    18.         var clamp = transform.position;
    19.         clamp.x =  Mathf.Clamp(transform.position.x, -transform.position.x - clampDist*3, clampDist);
    20.         clamp.y =  Mathf.Clamp(transform.position.y, -transform.position.y - clampDist*3, clampDist);
    21.         transform.position = clamp;
    22.     }
    It works, but is far from what VR is like, and also it is very jumpy when you hit the clamps.
    The other try was using rotation like I said:
    Code (CSharp):
    1. public Vector3 mousePos;
    2.     public float smoothTime = 0.3F;
    3.  
    4.     void Update()
    5.     {
    6.         mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
    7.         Vector3 targetRotation = mousePos + new Vector3(0, 0, 0);
    8.  
    9.         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(targetRotation), smoothTime);
    10.     }
    This is much, much similar to VR headset style movement however it is much more finicky (doesn't always go the direction of the mouse) and it is also quite jumpy when it does change direction.

    How can I improve this code to make it work like described?

    Thanks,
    Dream
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    VR doesn't constrain movement. It would be too nauseous for the player wearing a headset to have constrained movement. Imagine moving your head to the right, suddenly the display stops but your neck keeps twisting, it would be very weird to experience that. Nonetheless, I'll try to help you out here.

    From what I see, you essentially want just a first-person camera? I mean, that's what VR is.
    Does this help? https://answers.unity.com/questions/149022/how-to-make-camera-move-with-the-mouse-cursors.html
     
  3. DreamingInsanity

    DreamingInsanity

    Joined:
    Nov 5, 2017
    Posts:
    101
    Yep, awesome, that has done it.

    Thanks,
    Dream
     
    Dextozz likes this.