it's a bit tricky.
the problem is that the target object is moving and bouncing so if i will make the freelook camera to lookat or follow the moving object the camera will also bounce and i don't want it.
what i want to do is the first part just like it is now the camera is moving behind the player while the player is looking at the moving target. then when the camera is behind the player the moving object is getting out of the screen area. the player head is still looking at it but the camera keep looking and follow the player.
i want somehow to make that the camera will rotate looking at the moving object but from the player point of view and not that the camera will follow/lookat the moving object.
here is a screenshot at the point the moving object(in red circle) is start moving out of the screen :
now i'm not sure how to move to the next part.
at this point i want the camera to stay behind the player and rotate looking at the moving object "like" through the player eyes.
i tested it again now and while the object is moving and the camera is behind the player like in the screenshot i can rotate the camera with the mouse like rotating the camera with the player head looking at the moving object the question how to do it by script ?
i want to rotate the camera with the player head to look at the moving object through the player point of view so the camera will not get the moving object bouncing and other effects.
This screenshot explain what i mean that the camera is still looking at the player and following the player but viewing with the player at the moving object. in this case i'm rotating the freelook camera with my mouse but i want to do it in the script.
CodePudding user response:
well not sure what you are using currently as there is no code provided here ... but instead of directly passing in the target object's position simply use a vector where x
and z
come from your target object while y
comes from your player object like e.g.
// not necessarily the player itself but the usual target object for your camera
Transform player;
Transform camera;
Transform lookTarget;
camera.position = /*Just the usual way how you get your camera position*/;
var targetPosition = lookTarget.position;
targetPosition.y = player.position.y;
// Snapping see https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
camera.LookAt(targetPosition);
// or if you are e.g. using smooth interpolation
var targetRotation = Quaternion.LookRotation(targetPosition - camera.position);
camera.rotation = Quaternion.Slerp(camera.rotation, targetRotation, 5f * Time.deltaTime);