- Home /
Camera - Keep two objects on the screen while staying behind another
Hello everyone. I'm pretty stuck with something. I'm doing a school assignment where we have to recreate 10 seconds of a game. I have to recreate Kingdom Hearts, a fight between Wakka and Sora.
I'm stuck with the camera though, it rotates with the player, while looking at the middle of the two characters.
I've got a way figured out, but right now there are a couple problems.
When the camera reaches the location where it has to look, it pretty much breaks and turns 180 degrees every frame, not moving to any direction.
The camera moves at a constant speed when the player is out of bounds, this means that it's really stuttery.
While this method works in a way, it doesn't feel good at all, since it's so stuttery.
Here's the code I use:
void LateUpdate ()
{
var lookAtPoint = target1.position + target2.position;
lookAtPoint.x /= 2;
lookAtPoint.y /= 2;
lookAtPoint.z /= 2;
Vector3 velocity = new Vector3();
// horizontal movement
Vector2 screen = Camera.main.WorldToScreenPoint(target1.position);
float w = screen.x / Screen.width;
if (w < horizontalBounds) velocity.x = -_cameraSpeed;
else if (w > 1 - horizontalBounds) velocity.x = _cameraSpeed;
// vertical movement
Vector3 vectorToPlayer = target1.position - transform.position;// - target1.position;
float distance = Vector3.Project(vectorToPlayer, transform.forward).magnitude;
if (distance > maxDistance) velocity.z = _cameraSpeed;
else if (distance < minDistance) velocity.z = -_cameraSpeed;
Vector3 vel = transform.right * velocity.x * Time.deltaTime
+ transform.forward * velocity.z * Time.deltaTime;
wantedPosition += vel;
transform.position = Vector3.Slerp(transform.position,
wantedPosition, Time.deltaTime * _cameraSpeed);
transform.LookAt(target2);
}
Is there a way to fix this, or a better way to do this? Thanks in advance.
Answer by text23d · Nov 30, 2017 at 06:30 PM
maybe try going to Animation tab, make a new animation there for your camera, and try playing with variables like position, rotation of your camera. and don't forget to press 'record' button. also try https://www.assetstore.unity3d.com/en/?&_ga=2.140184123.92653374.1512065481-54233751.1511523262#!/content/79898