Casting A ray from one specific object that is somewhere in game(probably not at origin) to another

I’m trying to set up a situation where an object is constantly tracking the player in the scene “like a camera”. that is if he is in range and there isn’t any object in the way. I have been busting my head open trying to find out how to do this for a couple of days and I think I need some assistance on how to go about doing this. Every time I try going about doing it always just is off by either a bit or a lot.

        //ONLY WORKS IF AT 0, 0
        targetDir = player.transform.position - transform.position;
        Vector3 forward = transform.forward;
        rotation = Vector3.SignedAngle(targetDir, forward, Vector3.up);
        int layerMask = 1 << 8;
        layerMask = ~layerMask;
        RaycastHit hit;

        if (rotation >= -90 && rotation <= 90)
        {
            if (Vector3.Distance(transform.position, player.transform.position) < 25)
            {
                if (Physics.Raycast(transform.position, (player.transform.position - transform.position), out hit, 25))
                {
                    Debug.DrawRay(transform.position, hit.point, Color.red);
                    if (hit.transform.gameObject.name.Equals("player"))
                    {
                        print("I can see you");
                    }
                }
                else if (Physics.Raycast(transform.position, (player.transform.position - transform.position), out hit, 25, layerMask))
                {
                    Debug.DrawRay(transform.position, hit.point, Color.blue);
                    if (hit.transform == player.transform)
                    {
                         print("Something is in the way");
                    }
                }


            }

Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.

If you want to actually debug the camera code you already have above, here is an approach you can use to get more information.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Thanks but I had forgot to mention that this was not a camera in the scene. Its acting like a security camera but it is just an object. Thanks for the suggestion ill try printing out some data so I better find out the problem

Another useful thing is to create a 3D marker in the game and move it around with your camera controller.

eg:

  • read the player position
  • when you aim the camera move this little 3D marker to where you think you’re looking

Then I would expect it to appear precisely dead center in security camera. if it does not, why not?

I usually just use this to make markers:

GameObject myMarker;

void Start()
{
  myMarker = GameObject.CreatePrimitive( PrimitiveType.Sphere);
  Destroy( myMarker.GetComponent<Collider>());
}

// TODO anywhere else in your script, set myMarker.transform.position

This function seems to be what you want to do. Unity - Scripting API: Transform.LookAt

It seems that the example in the docs is what you want to do, so just follow that. You have to add logic to stop it when out of view and range.