Question by
NicolajFunch · Jan 12, 2018 at 10:47 AM ·
camerawallthrough
How do i avoid my camera going through walls and objectives?
Hey, i'm pretty new at programing and unity, and my camera is locked to my character and rotating with him, but its going through my walls and trees, what can i do?
my Script to camera is here:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Camera_Follow_Player : MonoBehaviour {
[SerializeField]
private Transform target;
[SerializeField]
private Vector3 offsetPosition;
[SerializeField]
private Space offsetPositionSpace = Space.Self;
[SerializeField]
private bool lookAt = true;
private void LateUpdate()
{
Refresh();
}
public void Refresh()
{
if (target == null)
{
Debug.LogWarning("Missing target ref !", this);
return;
}
// compute position
if (offsetPositionSpace == Space.Self)
{
transform.position = target.TransformPoint(offsetPosition);
}
else
{
transform.position = target.position + offsetPosition;
}
// compute rotation
if (lookAt)
{
transform.LookAt(target);
}
else
{
transform.rotation = target.rotation;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Determine camera distance for object to be completely visible 2 Answers
Why does my transform.lookat not work? 1 Answer
Changing camera angle and screentoworld 1 Answer
Cinemachine crashes unity 1 Answer
Pixel to z-Distance in Unity 0 Answers