- Home /
Question by
Sanerhp228yt · Apr 21, 2019 at 08:07 PM ·
camerafollow player
Camera rotating around player, but passes trough objects
Help! My camera rotates around a player, but passes through other objects (for example, walls). What do I need to do so that the camera is pushed of them off like a collider (here’s the script): using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerFollow : MonoBehaviour {
public Transform PlayerTransform;
private Vector3 _cameraOffset;
[Range(0.01f, 1.0f)]
public float SmoothFactor = 0.5f;
public bool LookAtPlayer = false;
public bool RotateAroundPlayer = true;
public float RotationsSpeed = 5.0f;
// Use this for initialization
void Start () {
_cameraOffset = transform.position - PlayerTransform.position;
}
// LateUpdate is called after Update methods
void LateUpdate () {
if(RotateAroundPlayer)
{
Quaternion camTurnAngle =
Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);
_cameraOffset = camTurnAngle * _cameraOffset;
}
Vector3 newPos = PlayerTransform.position + _cameraOffset;
transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
if (LookAtPlayer || RotateAroundPlayer)
transform.LookAt(PlayerTransform);
}
}
Comment
Your answer
Follow this Question
Related Questions
How To Make Camera Follow Bullet? 2 Answers
camera following player on path 2 Answers