0

I am trying to make a 3rd person shooter but keep getting an error right at my line about raycasting. I am trying to raycast from the center with a small ball representing where the ray lands

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class Camera : MonoBehaviour
{
    public Transform orientation;
    public Transform player;
    public Transform playerObj;
    public Rigidbody rb;
    public float rotationSpeed;
    [SerializeField] private LayerMask aimColliderLayerMask = new LayerMask();
    [SerializeField] private Transform debugTransform;
    public CameraStyle currentStyle;
    UnityEngine.Camera cam;
    public Transform CombatLookAt;
    public enum CameraStyle
    {
        Basic,
        Combat
    }
    void Start()
    {
        currentStyle = CameraStyle.Combat;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
        
    }

    // Update is called once per frame
    void Update()
    {

        Vector3 viewDir = player.position - new Vector3(transform.position.x, player.position.y, transform.position.z);
        orientation.forward = viewDir.normalized;


        if (currentStyle == CameraStyle.Basic)
        {
            float horizontalInput = Input.GetAxis("Horizontal");
            float verticalInput = Input.GetAxis("Vertical");
            Vector3 inputDir = orientation.forward * verticalInput + orientation.right * horizontalInput;


            if (inputDir != Vector3.zero)
            {
                playerObj.forward = Vector3.Slerp(playerObj.forward, inputDir.normalized, Time.deltaTime * rotationSpeed);
            }
        }
        else if (currentStyle == CameraStyle.Combat)
        {
            Vector3 dirToCombatLookAt = CombatLookAt.position - new Vector3(transform.position.x, CombatLookAt.position.y, transform.position.z);
            orientation.forward = dirToCombatLookAt.normalized;
            playerObj.forward = dirToCombatLookAt.normalized;
        }
        Vector2 screenCenterPoint = new Vector2(Screen.width / 2, Screen.height / 2);
        Ray ray = cam.ScreenPointToRay(screenCenterPoint);
        if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, aimColliderLayerMask))
        {
            transform.position = raycastHit.point;
            debugTransform.position = raycastHit.point;
        }
    } 
}

The error occurs at the "Ray ray =cam.ScreenPointtoRay" line and states that the object reference is not set to an instance of the object. I tried switching "cam.ScreenPointtoRay" to "Camera.main.ScreenPointtoRay" but that also does not work

1 Answers1

0

I think someone already mentions this in the comments but here is some more help.

The error you're encountering is due to the fact that you haven't assigned a value to the cam variable, which represents the camera you want to use for raycasting. In your code, there's no assignment to the cam variable before you're using it to create the ray.

To fix this issue, you need to assign the appropriate camera to the cam variable. You can do this in the Start method by using the Camera.main reference or by finding the camera by its tag or name, depending on your scene setup. Here's how you can modify your code to fix this issue:

void Start()
{
    currentStyle = CameraStyle.Combat;
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;

    // Assign the appropriate camera to the 'cam' variable
    cam = Camera.main; // or find the camera by tag, name or make it public and set it in inspector.
}

Replace Camera.main with the reference to the camera you want to use for raycasting. If you have multiple cameras in your scene, you might need to find the correct camera based on your scene structure.

After making this change, your raycasting line should work without the "object reference not set to an instance of the object" error.

I am not a professional, but if you have tried this already and still can't find the issue, you might want to share more of your code or show screenshots how all your GameObjects are set up to make it easier to determine or replicate!

Finlay
  • 76
  • 8