Raycast with Distance - Distance is wrong?

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);
    }



}

try drawing the rays with
Debug.DrawRay or Debug.DrawLine
to see where its coming from and where its pointing

1 Like

Thanks for the tip.

looks like the ray is working in a different coord system vs the coords im feeding it… very unexpected…
the cam focus and target are represented by blank gameobjects that children of a gameobject that also holds the character. this way moving/rotating the character also moves these points.

however the ray appears to be drawing relative to 0,0,0 and gets bigger the further from the world center you get.

again… unexpected…
:slight_smile:

it appears the Ray is using local position… even thought everything im feeding it world position.
The cam target and cam focus vectors are children of a blank game object. But they move and draw exactly where they should.

the ray however doesn’t

Any ideas on how to fix this is appreciated.

j

Can convert local/world position with, if thats the issue,

but maybe your ray should have origin and direction:
Ray(Vector3 origin, Vector3 direction)

1 Like

Hi Thanks. seems my issue was a missunderstanding on the requirements for the 2nd param of the Ray class.
Im experimenting with it now :slight_smile:

just use linecast between camera/target

Hi James.

good suggestion but not exactly what im trying to script.

there are times i want the player to “run ahead” and the camera may loose sight of them briefly. thats ok. But once the camera catches up if its settles in a bad a spot i want it to adjust to have a clear view of the player.