- Home /
How to move camera position to player object's direction while having looking at object?
I want to move camera behind my player to look into direction player is looking. This should happen after player movement is stopped. Right now, camera stays only in one direction and moves with Player. The trigger is a RotateCameraToPlayer function which is being called from Player script, once player movement is zero. I can receive EulerAngle of Object in Camera Script. I tried lot of options but Camera Rotation is conflicting with LookAt function and does not let camera rotate. Can moving position of camera work here? How can we calculate position from angle? I have commented position range needed on different camera angle.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
// Start is called before the first frame update
public GameObject target;
public float smoothSpeed = 3f;
public float smoothRotateSpeed = 30f;
public Vector3 offset = new Vector3(0f,2f,-9f);
public bool LookAtPlayer = true;
public float RotationsSpeed = 30f;
private Vector3 _cameraOffset;
private bool _isRotate = false;
private void LateUpdate() { // if there is a jerk, replace with private void FixedUpdate() {
if(LookAtPlayer == true){
MoveCameraToPlayer();
}
}
public void MoveCameraToPlayer(){
Vector3 desiredPosition = target.transform.position + offset;
Vector3 smoothedPosition = Vector3.Slerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
transform.position = smoothedPosition;
transform.LookAt(target.transform, target.transform.up);
}
public void RotateCameraToPlayer(Vector3 _angle){
Debug.Log(_angle);
// Location value Z > 0(-7) to 180(7) > 180 to 360
// Location value Y 3
// Location value X > 90 (-7) to 270 (7)
}
}
Your answer
Follow this Question
Related Questions
How to prevent camera from clipping through walls with raycasts? 0 Answers
Problems making a smash bros like camera 2 Answers
Getting the camera to move in the direction its facing. 0 Answers
How to make a camera Follow an Object moving in zigzag path? 1 Answer
Need help updating camera position to go above the player 2 Answers