Skip to main content
Bumped by Community user
added 54 characters in body
Source Link

I've tried to do a gun mechanic for my 3d game but when I tried to write ray cast it didn't work only layer that is printed is 0 even though it shoots and gets stopped by an obstacle here's the important code

private void Update()
{
    if (!Input.GetButton("Fire1") || !(Time.time > nextFire))
    {
        return;
    }
    nextFire = Time.time + fireRate;
    Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.5f));
    laserLine.SetPosition(0, gunEnd.position);
    if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out var hit, weaponRange))
    {
        print(hit.collider.gameObject.layer);
        if (hit.collider.gameObject.layer == enemyLayer)
        {
            laserLine.SetPosition(1, hit.point);
            //decrease enemy health
        }
        else
        {
            laserLine.SetPosition(1, rayOrigin + fpsCam.transform.forward * weaponRange);
        }
    }
}

all of the variables are serialaized on inspector window and no mistakes were made . i even checked if the enemy did have a working collider which it did and the layer output was 3(enemy)

I've tried to do a gun mechanic for my 3d game but when I tried to write ray cast it didn't work only layer that is printed is 0 even though it shoots and gets stopped by an obstacle here's the important code

private void Update()
{
    if (!Input.GetButton("Fire1") || !(Time.time > nextFire))
    {
        return;
    }
    nextFire = Time.time + fireRate;
    Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.5f));
    laserLine.SetPosition(0, gunEnd.position);
    if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out var hit, weaponRange))
    {
        print(hit.collider.gameObject.layer);
        if (hit.collider.gameObject.layer == enemyLayer)
        {
            //decrease enemy health
        }
        else
        {
            laserLine.SetPosition(1, rayOrigin + fpsCam.transform.forward * weaponRange);
        }
    }
}

all of the variables are serialaized on inspector window and no mistakes were made . i even checked if the enemy did have a working collider which it did and the layer output was 3(enemy)

I've tried to do a gun mechanic for my 3d game but when I tried to write ray cast it didn't work only layer that is printed is 0 even though it shoots and gets stopped by an obstacle here's the important code

private void Update()
{
    if (!Input.GetButton("Fire1") || !(Time.time > nextFire))
    {
        return;
    }
    nextFire = Time.time + fireRate;
    Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.5f));
    laserLine.SetPosition(0, gunEnd.position);
    if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out var hit, weaponRange))
    {
        print(hit.collider.gameObject.layer);
        if (hit.collider.gameObject.layer == enemyLayer)
        {
            laserLine.SetPosition(1, hit.point);
            //decrease enemy health
        }
        else
        {
            laserLine.SetPosition(1, rayOrigin + fpsCam.transform.forward * weaponRange);
        }
    }
}

all of the variables are serialaized on inspector window and no mistakes were made . i even checked if the enemy did have a working collider which it did and the layer output was 3(enemy)

Source Link

Raycast collision not working

I've tried to do a gun mechanic for my 3d game but when I tried to write ray cast it didn't work only layer that is printed is 0 even though it shoots and gets stopped by an obstacle here's the important code

private void Update()
{
    if (!Input.GetButton("Fire1") || !(Time.time > nextFire))
    {
        return;
    }
    nextFire = Time.time + fireRate;
    Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.5f));
    laserLine.SetPosition(0, gunEnd.position);
    if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out var hit, weaponRange))
    {
        print(hit.collider.gameObject.layer);
        if (hit.collider.gameObject.layer == enemyLayer)
        {
            //decrease enemy health
        }
        else
        {
            laserLine.SetPosition(1, rayOrigin + fpsCam.transform.forward * weaponRange);
        }
    }
}

all of the variables are serialaized on inspector window and no mistakes were made . i even checked if the enemy did have a working collider which it did and the layer output was 3(enemy)