Let the Camera Face the middle of the Map while still following the Player?
Hey,
I would like let my Camera follow my Player with an offset and keep the Player in the cameras center. I allready accomplished that with the following script:
public class CameraFollow : MonoBehaviour {
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offset;
private void LateUpdate()
{
Vector3 desiredPosition = target.position + offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
this.transform.position = smoothedPosition;
}
}
But on top of that i want the camera to look at the center of the map which is a mountain in this case. So that, if i walk around the mountain, the camera is always rotating with the maps centerpoint. I already tried making something like camera.lookat(map.center), but that didnt worked as expected, because the camera was focusing the maps center, but the player, after walking a few meters, wasnt centered anymore.
Hope you can help. Thanks!
Answer by tormentoarmagedoom · May 07, 2018 at 11:32 AM
Good day.
You need to follow this steps:
1 - Create a emptyobject, child of player, with the camera as child of this gameobject (hicheracy like this):
Player
>CameraCointainer
>Camera
black point is the player, square is the camera container, blue is the camera
Then the pivot position of Cameracontainer must be the player position. This way, the cameracontainer can rotate and the camera will be facing always the player.
You only need to know which rotation also looks at the mountain, so:
CameraContainer.transform.LookAt(mountain);
should work.
Bye!!
Thank you! I just ran in another problem ... After i made the camera rotate around the middle, the movement was still wrong, because it was relative to the world. I fixed that by changing is to be relative to the camera-transform. Now the movement is correct, but the player is still facing in wrong directions. $$anonymous$$aybe you could help me with that aswell?
Here is my Code:
void Update () { Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); Vector2 inputDir = input.normalized;
Vector2 speed = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick);
if (speed.x > 0.6 || speed.x < -0.6 || speed.y > 0.6 || speed.y < -0.6)
{
walking = false;
movementSpeed = runSpeed;
}
else
{
walking = true;
movementSpeed = walkSpeed;
}
if (!isInteracting)
{
transform.Translate(transform.forward * currentSpeed * Time.deltaTime, cameraPosition);
currentSpeed = $$anonymous$$athf.SmoothDamp(currentSpeed, movementSpeed * inputDir.magnitude, ref speedSmoothVelocity, speedSmoothTime);
float animationSpeedPercent = ((walking) ? 1f : 2f) * inputDir.magnitude;
animator.SetFloat("Forward", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
Vector3 lookRotation = new Vector3(input.x, 0, input.y);
if (lookRotation != Vector3.zero)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookRotation), 0.15F);
}
}
what rotation is wrong? I saw your answer about this, i answered there, maybe you just need to differnce between rotation and localrotation.
and as you can do this with positions, think in doing the same for rotations: If you have Objects A and B, and need the direction of B seen from A.position,
relative position = B.position - A.position
so
relative rotation = B rotation - A rotation
As i dont know what rotation you want/nned or how the objects are related, i can not help you... youneed to think and solve it by yourself!
Bye! Good luck!