Advertisement

Keep object in camera view

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

Firstly well done for not getting wound up by my rant, major plus points, it shows considerable strength of character, most people are hugely overly sensitive to criticism. :)

Secondly that description is far easier for us to understand. Out of interest what are the objects going to be in the area? Are they planets, and are they going to be moving? The objects are a collection of weapons. They are static and have no AI. 

Using a reference point for the camera target sounds sensible, I'm now thinking of it as a follow cam, kind of like a third person camera.

The kind of things I'd be thinking of storing to calculate my camera would be:

  • Target position (vector3)
  • Zoom (camera distance from target)
  • Yaw (defined relative to target)
  • Pitch (defined relative to target)

This is all made much easier because there exists a unity function for rotating a camera to face a particular location:

Quaternion.LookRotation

This takes a look direction and a reference up direction.

So what you need to do is

  1. Calculate the camera position
  2. Calculate the vector from the camera to the target (ptTarget - ptCamera)
  3. Use the Quaternion.LookRotation to return a quaternion which you can set as the camera rotation

The most interesting bit is calculating the camera location based on the information I stored earlier. There's a few ways of doing this but here is one:

  1. You can do this by starting with a unit vector (vector of length 1) pointing out along your 'default' ground axis (where yaw = 0). This all depends how you define your map, I forget how unity does its axes, I usually use xy for ground and z is up.
  2. But essentially you can rotate your unit vector up by the pitch (there will be unity functions for rotating vectors along x, y and z), THEN rotate this result vector using the yaw.
  3. This gives you the direction to the camera. You now need to scale this vector by the distance to the camera (zoom). You can do this simply by multiplying the unit vector (it should still be length 1) by the distance to the camera. If for some reason your direction might not be length 1, you can use the Normalize function to change the length to 1 while keeping the same direction.
  4. So the camera location will be the target location PLUS this vector to the camera.

Set the translate of the camera to this location, set the rotate to the quaternion given by Quaternion.LookRotation, and you are in business.

There are other ways of doing things too, you can zoom by changing the field of view of the camera instead of modifying the distance. This can give a 'Hitchcock dolly zoom' effect if you modify it with distance at the same time.

Note this is assuming a perspective camera. If you want to use orthographic, the distance of the camera from the target doesn't matter, just the orthographic scale you use to determine the camera matrix. The camera I am using is a perspective camera. 

Constraining the camera zoom can be done by capping the min / max distance. And constraining the target point is easy too, just do something like



if (x > x_max) x = x_max;
if (x < -x_max) x = -x_max;

for each axis, to get a bounding box, or for a sphere:



vector3 ptOffset = ptTarget - ptWorldCentre;
float dist =  ptOffset.Normalize(); // returns length

if (dist > max_dist)
{
	ptOffset *= max_dist;
	ptTarget = ptWorldCentre + ptOffset;
}

That's most of it, there's also determine which direction to move the target point when you track the view, but I'll leave that for the others to cover, too tired tonight! :)

 

*Update* 

I have done further research into how I can solve the problem I am having. I felt as if I went on a tangent when in fact am trying to develop a first person camera controller that can move forwards/backwards, look up/down and left/right. There are several tutorials on developing a first person camera controller, however I believe they are not fit for what I am trying to achieve. 

The first person camera controller I am trying to develop must be usable on touch screens (iOS and Android), and be based on inputs from the users fingers not joysticks.   

This topic is closed to new replies.

Advertisement