Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerShoot : MonoBehaviour
{
    public PlayerController PlayerController;
    public Camera camera;

    public float damage;

    public float canShoot = true;
    WaitForSeconds shootDelay = new WaitForSeconds(0.2f);

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        Shoot();
    }

    void Shoot()
    {
        if (Input.GetMouseButtonDown(0))
        {
            PlayerController.animator.SetBool("Shoot", true);
        }

        if (Input.GetMouseButtonUp(0))
        {
            PlayerController.animator.SetBool("Shoot", false);
        }

        if (Input.GetMouseButton(0) && canShoot) 
        {
            StartCoroutine(ShootDelayCoroutine());
            RaycastHit hit;
            if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit))
            {
                print("Enemy Takes Damage : " + hit.collider.gameObject.name);
                if (hit.collider.GetComponent())
                {
                    hit.collider.GetComponent().TakeDamage(damage);
                }
            }
        }

        IEnumerator ShootDelayCoroutine()
        {
            canShoot = false;
            yield return shootDelay;
            canShoot = true;
        }
    }
}


What I have tried:

<pre>how to fix error CS0029: Cannot implicitly convert type 'bool' to 'float' and  error CS0019: Operator '&&' cannot be applied to operands of type 'bool' and 'float'

I'm still learning and hope you can help me with my problem?
Posted
Updated 4-Apr-22 20:00pm
v2

1 solution

You have
C#
public float canShoot = true;
. So you've declared a member variable to be of type floating point, but then try to assign a boolean value (i.e. true) to it, which makes no sense. It's like declaring a member of type 'color' and then trying to assign a value of "Turing" to it (unless in your world "Turing" is a color...). Changing the type of canShoot from float to bool should fix both the compiler errors you have listed here.
 
Share this answer
 
Comments
CPallini 5-Apr-22 2:02am    
5.

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