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:
public Vector3 mousePos;
public Transform origin;
public float clampDist;
Vector3 velocity = Vector3.up;
public float smoothTime = 0.3F;
void Update()
{
var mouseCentered = Input.mousePosition;
mouseCentered.x -= Screen.width/2;
mouseCentered.y -= Screen.height/2;
mousePos = new Vector3(mouseCentered.x / 10, mouseCentered.y / 10, 0);
Vector3 pos = new Vector3(origin.position.x, origin.position.y, 0) + mousePos;
transform.position = Vector3.SmoothDamp(transform.position, pos, ref velocity, smoothTime);
var clamp = transform.position;
clamp.x = Mathf.Clamp(transform.position.x, -transform.position.x - clampDist*3, clampDist);
clamp.y = Mathf.Clamp(transform.position.y, -transform.position.y - clampDist*3, clampDist);
transform.position = clamp;
}
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:
public Vector3 mousePos;
public float smoothTime = 0.3F;
void Update()
{
mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
Vector3 targetRotation = mousePos + new Vector3(0, 0, 0);
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(targetRotation), smoothTime);
}
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