Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to add Raycast for shooting by following a tutorial on youtube but I still get 2 CS0029 Error Messages the first one saying


Assets\Shooting.cs(19,30): error CS0029: Cannot implicitly convert type 'bool' to 'UnityEngine.RaycastHit'


And the second one saying


Assets\Shooting.cs(21,13): error CS0029: Cannot implicitly convert type 'UnityEngine.RaycastHit' to 'bool'



here is my script



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooting : MonoBehaviour
{
    public Transform firePoint;

    void Update() 
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }

    void Shoot()
    {
        RaycastHit hitInfo = Physics.Raycast(firePoint.position, firePoint.right);

        if (hitInfo)
        {
            Debug.Log(hitInfo.transform.name);
        }
    }
}


What I have tried:

I tried to look for spelling mistakes but i LITTERALLY have the same script as in the tutorial one.
Posted
Updated 2-Aug-20 9:46am
v4
Comments
Sandeep Mewara 2-Aug-20 15:41pm    
Posting the code that is raising the error would be of more help here. Believe line 15-25 from file shooting.s should be good enough.
Hand Cubing 2-Aug-20 15:46pm    
i accidentally forgot to post the script. Now I added it
Sandeep Mewara 2-Aug-20 15:50pm    
Tutorial might be old. Learn using IDE, intellisense in it would have given clue if not error on what is the method return type.

1 solution

Here, have a look: Unity - Scripting API: Physics.Raycast[^]
Physics.Raycast returns a boolean type. You have assigned that to variable of type RaycastHit and thus the error.

It straight away returns true if the ray intersects with a Collider, otherwise false.

Thus,
C#
void Shoot()
{
  if (Physics.Raycast(firePoint.position, firePoint.right))
  {
    // Ray intersects
  }
}

Go through some documentation and article to learn about it, like:
How to Create a Raycast in Unity 3D | Studica Blog[^]


UPDATE:
A sample for you
C#
void FixedUpdate()
    {
        // Bit shift the index of the layer (8) to get a bit mask
        int layerMask = 1 << 8;

        // This would cast rays only against colliders in layer 8.
        // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
        layerMask = ~layerMask;

        RaycastHit hit;
        // Does the ray intersect any objects excluding the player layer
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
        {
            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
            Debug.Log("Did Hit");
        }
        else
        {
            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
            Debug.Log("Did not Hit");
        }
    }

Reference: Unity - Scripting API: Physics.Raycast[^]

Suggestion: Please go through the link above and examples there instead of trying to follow some youtube. Until you understand basics and how to use IDE, it would be difficult for you to follow.
 
Share this answer
 
v3
Comments
Hand Cubing 2-Aug-20 15:55pm    
One Error Message is gone but the first one is still here

Assets\Shooting.cs(19,30): error CS0029: Cannot implicitly convert type 'bool' to 'UnityEngine.RaycastHit'
Sandeep Mewara 2-Aug-20 16:08pm    
seems your error is still pointing at line: RaycastHit hitInfo = Physics.Raycast(firePoint.position, firePoint.right);

As shared, thats wrong assignment. do:
bool hitInfo = Physics.Raycast(firePoint.position, firePoint.right);

BTW, with that, Debug.Log(hitInfo.transform.name); will be wrong. hitinfo would be just a boolean.
Sandeep Mewara 2-Aug-20 16:11pm    
Updtaed answer with an example and suggestion.
Hand Cubing 2-Aug-20 16:11pm    
Thank you so much for saving my live :). Really appriciate your help!
Sandeep Mewara 2-Aug-20 16:17pm    
Your welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900