Advertisement

Keep object in camera view

Started by July 18, 2018 06:25 PM
31 comments, last by davejones 6 years, 1 month ago

Without looking at the code, here's an idea: on every update you want to transform the camera in some way, like moving, rotating or scaling it. If the camera + that transformation will cause the object to go out of the screen, then don't transform the camera on that update.
I think this type of logic is called "filtering". Using any tests you want, you are filtering out any transformations that will cause the camera to look away from one or more objects before applying the transformation.
(This works only if the objects are static.)

Another solution is to compensate \ adjust after the fact: do move the camera every update, but in case one or more objects go out of frame, compute a movement or rotation for that camera that will cause it to bring them all back into view (eg aim the camera at the median* of all object locations, then dolly the camera back until all objects fit within the visible volume).

*Use the median of their locations (the center of the sum of their bounds), rather than using their average locations (sum locations and divide by n), because if you use the average the camera will always focus on clustered objects rather than every object equally.

7 hours ago, davejones said:

So when the camera is being panned it can only move so far until it is blocked. 

You will want to find the distance from the camera to the side of the render border. Then using very basic triangulation you will limit how much the camera can turn. This is something used a lot in CGI.

 

If you don't know the math then you can manually calibrate it.  In Unity duplicate your object in focus, then move it till it reaches the edge of the screen. The angle from the object to the camera is your rotation limit. In Unity the bounds will also give you this rotation, just checked it is 45 degrees rotation by default.

So by limiting the rotation to say 40 degrees to can always have the object on screen.

Advertisement
43 minutes ago, Scouting Ninja said:

You will want to find the distance from the camera to the side of the render border. Then using very basic triangulation you will limit how much the camera can turn. This is something used a lot in CGI.

 

If you don't know the math then you can manually calibrate it.  In Unity duplicate your object in focus, then move it till it reaches the edge of the screen. The angle from the object to the camera is your rotation limit. In Unity the bounds will also give you this rotation, just checked it is 45 degrees rotation by default.

So by limiting the rotation to say 40 degrees to can always have the object on screen.

When I say panning I mean dragging/swiping the camera out of view of the object I want to keep visible. I am trying to prevent the ability to drag/Swipe out of sight of the object in focus. 

20 minutes ago, davejones said:

When I say panning I mean dragging/swiping the camera out of view of the object I want to keep visible. I am trying to prevent the ability to drag/Swipe out of sight of the object in focus. 

So by panning do you mean strictly linear motion, with no angular/rotational motion involved?

Am tired now but basically this is what I'd be thinking about:

  1. Let the original rotation of the camera be quaternion B.
  2. Use a lookat function to get a quaternion A from the camera position to the object centre.
  3. Use a slerp with a limit function to slerp from A to B. If B is within the limit it will stay at B, as the object will still be in view, otherwise it will move the camera view towards A.

I believe there are built-in versions of these functions in Unity, I'm pretty sure I used them in my Tower Defence game.

Quaternion.LookRotation, Quaternion.Slerp

Note that this won't take account of the size of the mesh at different magnifications, you might have to account for that.

14 hours ago, Zakwayda said:

So by panning do you mean strictly linear motion, with no angular/rotational motion involved?

When I say panning I mean when the user uses 2 fingers to slide in addition to angular/rotational motion from when the user drags one finger across the screen. So 1 finger on touch input = rotational motion

2 finger sliding touch input = panning 

The issue I am trying to solve is to try and limit the panning to a specific area. So the user can only pan within certain area meaning the target object never goes out of sight. The link attached shows the kind of panning I am trying to add to my project in addition to having the ability to carry out rotational movements. 

 

https://gfycat.com/gifs/detail/GlisteningUnselfishLeonberger

Advertisement
1 hour ago, davejones said:

When I say panning I mean when the user uses 2 fingers to slide in addition to angular/rotational motion from when the user drags one finger across the screen. So 1 finger on touch input = rotational motion

2 finger sliding touch input = panning 

Do you mean 'tracking' (is this the confusion?):

https://en.wikipedia.org/wiki/Panning_(camera)

Quote

Panning should never be confused with tracking or "travelling," in which the camera is not just swivelled but is physically displaced left or right, generally by being rolled parallel to its subject.

 

 

32 minutes ago, lawnjelly said:

Do you mean 'tracking' (is this the confusion?):

https://en.wikipedia.org/wiki/Panning_(camera)

 

 

I mean tracking/travelling yes. I do apologise for the confusion in terminology. So effectively have the camera tracking/travelling only within a bounded area as well as being able to rotate and zoom. 

Unity's Tanks demo has a self adjusting camera.

WebGL demo:
https://webassembly.org/demo/

Camera code:
https://unity3d.com/learn/tutorials/projects/tanks-tutorial/camera-control?playlist=20081

5 minutes ago, Kryzon said:

I want the camera to be kept within a certain area in 3D space so that when its it can't infinity move out of view of the target object. 

This topic is closed to new replies.

Advertisement