- Home /
Camera smoothing of a Child Camera
Hello,
I'm tyring to make a simple multiplayer spaceshooter. For the Spaceships I want a 3rd person camera just binhind and a little above the player ships. The Camera is atached to the Player Ship Prefab as an Child object.
Now I want a bit of smoothness in the camera's Motion Relative to its Parent object.
I only found some Scripts with Targets, but I have no Target, just looking forward.
Somebody knows a Solution for this? Smoothing the Cameras Position without a Target (only its Parent) to follow?
Answer by DaDonik · Jan 20, 2015 at 11:44 AM
I would not parent the camera to the spaceship. The way i normally do this kind of stuff is as follows:
1) Use the forward vector of the spaceship, multiply it by minus the distance you want the camera to stay behind the ship.
2) Do the same with the up vector of the ship.
Now you have a position where you want the camera to move to. All thats left to do is to lerp the camera towards this position.
This will do the job, in fact id does even more than requested :D
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public Camera TheCamera;
public Transform TargetToFollow;
public Vector3 DesiredOffset;
public float Speed;
void Update ()
{
// Calculate offset vector.
Vector3 v3TargetOffset = TargetToFollow.position;
v3TargetOffset += (DesiredOffset.z * TargetToFollow.transform.forward);
v3TargetOffset += (DesiredOffset.y * TargetToFollow.transform.up);
v3TargetOffset += (DesiredOffset.x * TargetToFollow.transform.right);
// Lerp the camera.
Vector3 v3CurrentCameraPos = TheCamera.transform.position;
v3CurrentCameraPos.x = Mathf.Lerp(v3CurrentCameraPos.x, v3TargetOffset.x, Speed * Time.deltaTime);
v3CurrentCameraPos.y = Mathf.Lerp(v3CurrentCameraPos.y, v3TargetOffset.y, Speed * Time.deltaTime);
v3CurrentCameraPos.z = Mathf.Lerp(v3CurrentCameraPos.z, v3TargetOffset.z, Speed * Time.deltaTime);
TheCamera.transform.position = v3CurrentCameraPos;
TheCamera.transform.LookAt(TargetToFollow.transform);
}
}
Thank you, it works well for me, i just make some changes.
I had problems with some stutter, it cam be solved by putting the code into the "FixedUpdate" function.
using UnityEngine;
using System.Collections;
public class CameraFollow : $$anonymous$$onoBehaviour
{
public Camera camera;
public Transform target;
public Vector3 offset;
public Vector3 viewPointOffset;
public float speed;
public bool ActivateSmoothRotation;
void FixedUpdate ()
{
if(camera && target)
{
Vector3 v3TargetOffset = target.position;
v3TargetOffset += (offset.z * target.transform.forward);
v3TargetOffset += (offset.y * target.transform.up);
v3TargetOffset += (offset.x * target.transform.right);
Vector3 v3viewPointOffset = target.position;
v3viewPointOffset += (viewPointOffset.z * target.transform.forward);
v3viewPointOffset += (viewPointOffset.y * target.transform.up);
v3viewPointOffset += (viewPointOffset.x * target.transform.right);
if(ActivateSmoothRotation)
{
Quaternion rotation = Quaternion.LookRotation(v3viewPointOffset - camera.transform.position);
Quaternion smoothRotation = Quaternion.Slerp(camera.transform.rotation, rotation, Time.deltaTime * speed);
camera.transform.rotation = smoothRotation;
}
camera.transform.position = Vector3.Lerp(camera.transform.position, v3TargetOffset, Time.deltaTime * speed);
if(!ActivateSmoothRotation)
camera.transform.LookAt(v3viewPointOffset);
}
}
}
Hello again, is there a way to apply the Z rotation to the Camera? If I make
smoothRotation.z = target.rotation.z;
I got a very Strange behavior If i Rotate the ship around this Axis.
Let me guess, thats only the case if you are using the LookAt function?
If so, the problem can be solved by using the second parameter of the LookAt function. Check the documentation here
So when using LookAt, you should not try to rotate the camera around the z-axis, ins$$anonymous$$d you should specify the up vector in the LookAt function.
ok, thats it Thank you again.
The second parameter works also with the Quaternion.LookRotation function.
I'm so proud of you, so new to Unity, yet such a quick learner :D
Your answer
Follow this Question
Related Questions
Why is my Camera rotating? 2 Answers
Smooth camera script looking at left hand side of character. 0 Answers
Child affecting parent rigidbody? 1 Answer
How to smoothen movement of a child object? 1 Answer
Target Box not bounding targets 1 Answer