I want to write follow camera script for player’s position and rotation both. At present I can able to write script so that it can follow player’s position but can’t able to write proper code for its rotation.
void Update ()
{
if (target == null)
return;
Vector3 targetCamPos = target.position + offset;
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
transform.LookAt (target);
// Vector3 targetCamRot = target.eulerAngles + offsetRot;
// transform.eulerAngles = Vector3.Lerp (transform.eulerAngles, targetCamRot, smoothing * Time.deltaTime);
}
I have used two approaches above but none of them worked for me.
In LookAt, I can’t able to set my desire rotation of camera.
In EulerAngles camera get rotated its own place rather than related its target.
If I put my camera object inside player’s child then its working properly, through this way I can able to get position and rotation change according to the player. But I want to do this through code rather than placing my camera under player game object.
Here is my game play over view:
This seems like a better place for this conversation than on the answers board.
What is your motivation for wanting the camera not to be a child of the player?
1 Like
Yes sir I don’t want to set camera as a child of player object.
I want to write script for following purpose.
But why is the question. You have various player controllers in unity. 3rd person controller is also present.
Can you refer me any link?
Basically I was facing problem in camera rotation.
I was tried with LookAt but in that I can’t able to give my custom rotation.
I want camera rotation also work as third person, at present position get followed but rotation don’t.
It would be nice if you answered the question we’ve asked twice.
Basically player get any damage or force then camera also get shaking based on this if I put camera child as player object.
Ah. Okay. Now we’re talking. You want to decouple the motion of the camera from motion of the player but still have one influence the other.
I think there’s a solution to this that doesn’t involve the implementation toward which you were driving us.
Create a parent object that is the thing that moves around, points, aims, gets controlled by the human player, et cetera.
Make one of the child objects of that parent object what you now call the “player” object. Make another child object the camera object. When the shaking happens, make it happen only to the child of the parent player object which is what the player sees. She shaking one affect the master context object and, therefore, won’t affect the camera object.
1 Like
Just jam your camera in its own hierarchy.
Camera Base
|-> Camera Pivot
|-> Actual Camera
Set the location of the base to the player’s location in the LateUpdate and set the Camera Pivot’s rotation to whatever you want.
3 Likes
If you have a moving target and you want to rotate the camera around it (with mouse pressed) at the same time that the camera is following the target, I have a camera script here. I was testing with a plane at y-pos=0 and a vehicle hovering above it at y-pos=1. That’s why I don’t allow the camera to go below y-pos=2.
public class CameraBehaviour : MonoBehaviour
{
public Transform target;
public float cameraDistance = 6f;
Vector3 currPosition;
Vector3 offsetFromTarget;
void FixedUpdate()
{
FollowTarget();
if (Input.GetMouseButton(0))
{
RotateMe();
}
}
void RotateMe()
{
currPosition = transform.position;
currPosition += Input.GetAxis("Mouse X") * transform.right;
currPosition += Input.GetAxis("Mouse Y") * transform.up;
if(currPosition.y < 2)
{
currPosition.y = 2;
}
transform.position = currPosition;
transform.LookAt(target);
}
void FollowTarget()
{
offsetFromTarget = (transform.position - target.position).normalized;
currPosition = target.position + (offsetFromTarget) * cameraDistance;
if (currPosition.y < 2)
{
currPosition.y = 2;
}
transform.position = currPosition;
transform.LookAt(target);
}
}