[SOLVED] Linecast and Raycast - "Cannot implicitly convert type `bool' to `UnityEngine.RaycastHit'"

Hi everyone, i’m trying to adapt the part from the ‘2d roguelike’ tutorial series about raycast/linecast collision detection from its initial 2D sprites to 3D.

I am unfortunately getting the following error:

error CS0029: Cannot implicitly convert type `bool' to `UnityEngine.RaycastHit'

This is the extract of code i am using (even though i cut out the irrelevant parts concerning the issue):

using UnityEngine;
using System.Collections;

public abstract class Movement : MonoBehaviour
{
    public LayerMask blockingLayer;

    private BoxCollider boxCollider;
    private Rigidbody rigidB;


    protected virtual void Start ()
    {
        boxCollider = GetComponent<BoxCollider>();
        rigidB = GetComponent<Rigidbody>();

    }

    protected bool Move (int xDir, int yDir, int zDir, out RaycastHit hit)
    {
        Vector3 startPos = transform.position;
        Vector3 endPos = startPos + new Vector3(xDir, yDir, zDir);
        boxCollider.enabled = false;
        hit = Physics.Linecast(startPos, endPos, blockingLayer);
        boxCollider.enabled = true;

        if (hit.transform == null)
        {
            StartCoroutine(SmoothMove(endPos));
            return true;
        }

        return false;
    }

Basically, from the original tutorial, i changed/skipped the 2D parts for the 3D equivalents.
I switched Vector2 to Vector3 and added the int for another axis.

I replaced the error free (i think at least)

out RaycastHit2D hit

with

out RaycastHit hit

and

hit = Physics2D.Linecast

with

hit = Physics.Linecast

Anyways, im new to Unity3D, CSharp, and the Forum too. I tried all day to figure out the reason for the error but with no success. Sorry if i might be a bit derpy concerning the issue, so please be gentle ;p

Help in trying to fix the error in code and or eliminating my misunderstandings of the concept are greatly appreciated.

The method Physics.Linecast will only return a bool. The methods from 2D to 3D change a bit.

There is an overloaded method that uses RaycastHit.

In your case you would need to use this

RaycastHit hit;
if (Physics.Linecast(startPos, endPos, out hit, blockingLayer)
{ }
3 Likes

Thank your very much for the rapid response.

I think it did the trick, i still have a bit of down the line changes inside my project as I was trying to add this into a project I was sort of coding earlier from scratch. Atleast for now, im error free : )
Indeed, in the documentation thats kind of how it looks like for the non2d-raycast… my bad.
I suppose ill try to read up more on overloaded methods as i still would like to figure out why exactly that is the case.

Thanks again and (at everyone else on here too) keep up the great work! : )

curvesandsymmetry, would you mind posting your implementation of Chris’s response?

I’m trying to do something similar with the roguelike movement script to move a 3D character a set distance for each key press and I did the same thing you originally did and got the exact same error, but I can’t figure out how Chris’s code fits into the rest of the script.

1 Like

I need you to put 2D on out Raycasthit
It would look like this “out raycasthit2D

4777601--455504--Screenshot_1.jpg
4777601--455507--Screenshot_2.jpg
4777601--455510--Screenshot_3.jpg
4777601--455513--Screenshot_4.jpg

1 Like