Im looking for advice / help on this one.
I’m building a simple camera system. There are three main points of reference
The camera target position - A point usually behind the main character
The realTarget - ie. the character
The focusTarget - A point usually in front of the character
As the player moves i do a Slerp to have the camera move back into the camera target position.
Now im adding in logic to test if an objects gets between the camera target position and the realTarget( character) if so ill adjust the camera to be closer.
Only it seems my ray, despite having a distance applied is going further and reach out in front of my character.
ie:
A wall in front of my character but NOT between the target position and real position is causing my raycast to having a hit.
the question is why is that? the distance applied is the lenght of real position to target position. Its almost like the ray cast is ignore the distance param.
any help is appreciated.
using UnityEngine;
using System.Collections;
public class CinemaCamera : MonoBehaviour {
public GameObject positionTarget; // target position for the camera to "hover" or move to
public GameObject realTarget; // the character (what we care to be in the camera view but not always the focus of the camera)
public GameObject focusTarget; // where we want the camera pointing.
public float followSpeed = 1f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void LateUpdate () {
Vector3 posTarget = positionTarget.transform.position;
// check for view obstructions
RaycastHit targetPosHit;
float rayDistance = Vector3.Distance (realTarget.transform.position, positionTarget.transform.position);
Ray ray = new Ray (realTarget.transform.position, positionTarget.transform.position);
if (Physics.Raycast (ray, out targetPosHit, rayDistance - 0.1f)) {
// something is blocking where the camera is suppose to be. so we need to set a temporary targetPosition
Debug.Log ("View is obstructed : "+targetPosHit.collider.gameObject);
posTarget = targetPosHit.point;
}
// move camera as needed.
transform.position = Vector3.Slerp (transform.position, posTarget, Time.deltaTime * followSpeed);
transform.LookAt (focusTarget.transform.position);
}
}