コード例 #1
0
    void Update()
    {
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;

            StartCoroutine(ShotEffect());

            Vector3    rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
            RaycastHit hit;

            laserLine.SetPosition(0, gunEnd.position);

            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                laserLine.SetPosition(1, hit.point);

                ShootableBox health = hit.collider.GetComponent <ShootableBox> ();

                if (health != null)
                {
                    health.Damage(gunDamage);
                }

                if (hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
コード例 #2
0
    // we will handle user input, check if the player can fire and ultimately apply physics force to any GameObject with a Rigidbody hit by the ray.
    void Update()
    {
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate; // set shooting delay
            StartCoroutine(ShotEffect());

            Vector3    rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
            RaycastHit hit;

            laserLine.SetPosition(0, gunEnd.position); // gunEnd is GameObject that we will attach to the end of our gun in the scene.

            // it will only be executed if our raycast hit something.
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                laserLine.SetPosition(1, hit.point);
                // check if there is a ShootableBox component attached to the GameObject that our raycast hit.
                ShootableBox health = hit.collider.GetComponent <ShootableBox>();

                if (health != null)
                {
                    health.Damage(gunDamage);

                    if (hit.rigidbody != null)
                    {
                        hit.rigidbody.AddForce(-hit.normal * hitForce);
                    }
                }
            }
            else
            {
                laserLine.SetPosition(1, fpsCam.transform.forward * weaponRange);
            }
        }
    }
コード例 #3
0
    void Explode()
    {
        // Instantiate explosion
        Instantiate(explosionEffect, transform.position, transform.rotation);

        // Create collision radius
        Collider[] collision = Physics.OverlapSphere(transform.position, radius);

        // For each object in the collision check it has a rigidbody
        foreach (Collider nearbyObject in collision)
        {
            Rigidbody rb = nearbyObject.GetComponent <Rigidbody>();
            if (rb != null)
            {
                rb.AddExplosionForce(force, transform.position, radius);
            }

            // Damage the enemy
            ShootableBox damage = nearbyObject.GetComponent <ShootableBox>();
            if (damage != null)
            {
                damage.Damage(3);
            }
        }
        // Remove Mine
        Destroy(gameObject);
    }
コード例 #4
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            StartCoroutine(ShotEffect());
            //seteamos el origen del láser al centro de la cámara
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
            //obtenemos la posición del objeto que apuntamos con esta variable
            RaycastHit hit;
            laserLine.SetPosition(0, gunEnd.position);
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                laserLine.SetPosition(1, hit.point);

                //verificamos si el objeto golpeado era un objetivo
                ShootableBox health = hit.collider.GetComponent <ShootableBox>();
                if (health != null)
                {
                    health.Damage(gunDamage);
                }
                if (hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
コード例 #5
0
    void Update()
    {
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;

            StartCoroutine(ShotEffect());
            //ışın başlangıcım var kamera merkezimi verdim
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
            //raycasthit imi tanımladım
            RaycastHit hit;
            //line renderer değişkenime pozisyon atıyorum (index ve pozisyon)
            laserLine.SetPosition(0, gunEnd.position);
            //burada rayorigin başlangıcından fpscam konumu düzleminde silah aralığım kadar ateş edicem
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                laserLine.SetPosition(1, hit.point);
                ShootableBox health = hit.collider.GetComponent <ShootableBox>();
                if (health != null)
                {
                    health.Damage(gunDamage);
                }
                if (hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
コード例 #6
0
ファイル: GunShooting.cs プロジェクト: Bhasura/Ice-n-Spice
    /*public void Update()
     * {
     *  if (Input.GetButtonDown("Fire1"))
     *  {
     *      ShootGun();
     *
     *      StartCoroutine(ShotEffect());
     *      rayOrigin = fpsCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
     *      laserLine.SetPosition(0, gunEnd.position);
     *  }
     * }*/

    public void ShootGun()
    {
        muzzleFlash.Play(); // particle system

        RaycastHit hit;

        if (Physics.Raycast(rayOrigin, fpsCamera.transform.forward, out hit, range)) // if the raycast has hit anything
        {
            laserLine.SetPosition(1, hit.point);
            ShootableBox enemy = hit.transform.GetComponent <ShootableBox>();
            if (enemy != null)
            {
                enemy.Damage(damage);
                // have to add animation each time hit
                GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(impactGO, 1f);
            }
            if (hit.rigidbody != null) // might not need this if flying - my enemies right now don't have rigidbodies
            {
                hit.rigidbody.AddForce(-hit.normal * impactForce);
            }
        }
        else
        {
            laserLine.SetPosition(1, rayOrigin + (fpsCamera.transform.forward * range));
        }
    }
コード例 #7
0
    public void Shoot()
    {
        // 在相机视口中心创建向量
        Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

        // 声明RaycastHit存储射线射中的对象信息
        RaycastHit hit;

        // 检测射线是否碰撞到对象
        if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
        {
            // 获取被射中对象上的ShootableBox组件
            ShootableBox health = hit.collider.GetComponent <ShootableBox>();

            // 如果组件存在
            if (health != null)
            {
                // 调用组件的Damage函数计算伤害
                health.Damage(gunDamage);
            }

            // 检测被射中的对象是否存在rigidbody组件
            if (hit.rigidbody != null)
            {
                // 为被射中的对象添加作用力
                hit.rigidbody.AddForce(-hit.normal * hitForce);
            }
        }
        else
        {
            // 如果未射中任何对象,则将射击轨迹终点设为相机前方的武器射程最大距离处
            //rayOrigin + (fpsCam.transform.forward * weaponRange)
        }
    }
コード例 #8
0
    void Update()
    {
        // Check if the player has pressed the fire button and if enough time has elapsed since they last fired
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            // Update the time when our player can fire next
            nextFire = Time.time + fireRate;

            // Start our ShotEffect coroutine to turn our laser line on and off
            StartCoroutine(ShotEffect());

            // Create a vector at the center of our camera's viewport
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

            // Declare a raycast hit to store information about what our raycast has hit
            RaycastHit hit;

            // Set the start position for our visual effect for our laser to the position of gunEnd
            laserLine.SetPosition(0, gunEnd.position);

            // Check if our raycast has hit anything
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                // Set the end position for our laser line
                laserLine.SetPosition(1, hit.point);

                // Get a reference to a health script attached to the collider we hit
                ShootableBox health = hit.collider.GetComponent <ShootableBox>();

                // If there was a health script attached
                if (health != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    health.Damage(gunDamage);
                }


                // Check if the object we hit has a rigidbody attached
                if (hit.rigidbody != null)
                {
                    // Add force to the rigidbody we hit, in the direction from which it was hit
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }

                ShootableUI shotUI;
                shotUI = hit.collider.GetComponent <ShootableUI> ();

                if (shotUI != null)
                {
                    shotUI.ShotClick();
                }
            }
            else
            {
                // If we did not hit anything, set the end of the line to a position directly in front of the camera at the distance of weaponRange
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
コード例 #9
0
    void Shoot()
    {
        if (Time.time > nextFire && canFire == true && !isReloading && currentMagazine > 0)
        {
            currentMagazine--;
            nextFire = Time.time + fireRate;
            StartCoroutine(ShotEffect());
            Vector3 rayOrigin = cam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

            RaycastHit hit;
            bulletLine.SetPosition(0, gunTip.transform.position);
            AudioSource.PlayClipAtPoint(gunClip, gunTip.transform.position);

            if (Physics.Raycast(rayOrigin, cam.transform.forward, out hit, weaponRange))
            {
                bulletLine.SetPosition(1, hit.point);
                ShootableBox          health            = hit.collider.GetComponent <ShootableBox>();
                EnemyController       enemyHealth       = hit.collider.GetComponent <EnemyController>();
                EnemySniperController enemySniperHealth = hit.collider.GetComponent <EnemySniperController>();
                FlakCannonController  flakCannonHealth  = hit.collider.GetComponent <FlakCannonController>();
                GameObject            impactHit         = Instantiate(ImpactShot, hit.point, Quaternion.identity) as GameObject;
                Destroy(impactHit, 1f);
                AudioSource.PlayClipAtPoint(impactClip, hit.point);

                if (health != null)
                {
                    health.Damage(gunDamage);
                }

                if (enemyHealth != null)
                {
                    enemyHealth.Damage(gunDamage);
                }

                if (enemySniperHealth != null)
                {
                    enemySniperHealth.Damage(gunDamage);
                }

                if (flakCannonHealth != null && damageType == "rocket")
                {
                    flakCannonHealth.Damage(gunDamage);
                }

                if (hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                bulletLine.SetPosition(1, rayOrigin + (cam.transform.forward * weaponRange));
            }
        }
    }
コード例 #10
0
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Enemy"))
        {
            ShootableBox health = other.GetComponent <ShootableBox>();

            // If there was a health script attached
            if (health != null)
            {
                // Call the damage function of that script, passing in our gunDamage variable
                health.Damage(bulletDamage);
            }
        }
    }
コード例 #11
0
    void Update()
    {
        // 检测是否按下射击键以及射击间隔时间是否足够
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            // 射击之后更新间隔时间
            nextFire = Time.time + fireRate;

            // 启用ShotEffect携程控制射线显示及隐藏
            StartCoroutine(ShotEffect());

            // 在相机视口中心创建向量
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

            // 声明RaycastHit存储射线射中的对象信息
            RaycastHit hit;

            // 将射击轨迹起点设置为GunEnd对象的位置
            laserLine.SetPosition(0, gunEnd.position);

            // 检测射线是否碰撞到对象
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                // 将射击轨迹终点设置为碰撞发生的位置
                laserLine.SetPosition(1, hit.point);

                // 获取被射中对象上的ShootableBox组件
                ShootableBox health = hit.collider.GetComponent <ShootableBox>();

                // 如果组件存在
                if (health != null)
                {
                    // 调用组件的Damage函数计算伤害
                    health.Damage(gunDamage);
                }

                // 检测被射中的对象是否存在rigidbody组件
                if (hit.rigidbody != null)
                {
                    // 为被射中的对象添加作用力
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                // 如果未射中任何对象,则将射击轨迹终点设为相机前方的武器射程最大距离处
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
コード例 #12
0
    // Most of our game logic goes here
    void Update()
    {
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;

            StartCoroutine(ShotEffect());

            // We always want this to be at the center of our camera
            // ViewToWorldPoint takes a point in ViewPort space and converts it to world space
            // Viewport space is normalized from 0,0 to 1,1, and the Z-axis moves the point out into the frustum (pyramid with tip chopped off)
            Vector3    rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
            RaycastHit hit;

            laserLine.SetPosition(0, gunEnd.position);

            // Now we will use physics.raycast to determine the endcast of our raycast
            // Later we will use physics.raycast to apply forces and deal damage
            // Physics.raycast returns boolean (hit == true)

            // out keyword allows us to store additional return info from a function
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                // if we fire into the sky, our raycast will hit nothing
                // if we hit something with the raycast, set the second position of our line to hit.point
                laserLine.SetPosition(1, hit.point);

                // Check if the thing we hit had a ShootableBox attached
                ShootableBox health = hit.collider.GetComponent <ShootableBox> ();

                if (health != null)
                {
                    health.Damage(gunDamage);
                }

                if (hit.rigidbody != null)
                {
                    // Hit.normal is emerging from the surface of the object we hit, in the direction of the protruding surface
                    // -Hit.normal allows us to apply appropriate force
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
コード例 #13
0
ファイル: RayCastShoot.cs プロジェクト: NoobPete/ludumdare44
    // Update is called once per frame
    void Update()
    {
        if (Input.GetButton("Reload") && !reloading)
        {
            reloading   = true;
            currentAmmo = 0;
            StartCoroutine(ReloadWait());
        }

        if (Input.GetButton("Fire1") && Time.time > nextFire && currentAmmo > 0)
        {
            currentAmmo--;

            nextFire = Time.time + fireRate;
            StartCoroutine(ShotEffect());

            // 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;

            Vector3    rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(.5f, .5f, 0));
            RaycastHit hit;
            shotMade = true;
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange, layerMask))
            {
                shotPosition = hit.point;

                ShootableBox health = hit.collider.GetComponent <ShootableBox>();
                if (health != null)
                {
                    health.Damage(gunDamage);
                }
                if (hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                shotPosition = fpsCam.transform.position + fpsCam.transform.forward * weaponRange;
            }
        }

        ammoText.text = currentAmmo + "/" + maxAmmo;
    }
    public void Fire()
    {
        //Create a vector at the center of our camera's near clip plane.
        Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(.5f, .5f, 0));

        //Draw a debug line which will show where our ray will eventually be
        Debug.DrawRay(rayOrigin, fpsCam.transform.forward * weaponRange, Color.green);

        //Declare a raycast hit to store information about what our raycast has hit.
        RaycastHit hit;

        //Start our ShotEffect coroutine to turn our laser line on and off
        StartCoroutine(ShotEffect());

        //Set the start position for our visual effect for our laser to the position of gunEnd
        laserLine.SetPosition(0, gunEnd.position);

        //Check if our raycast has hit anything
        if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
        {
            //Set the end position for our laser line
            laserLine.SetPosition(1, hit.point);

            //Get a reference to a health script attached to the collider we hit
            ShootableBox health = hit.collider.GetComponent <ShootableBox>();

            //If there was a health script attached
            if (health != null)
            {
                //Call the damage function of that script, passing in our gunDamage variable
                health.Damage(gunDamage);
            }

            //Check if the object we hit has a rigidbody attached
            if (hit.rigidbody != null)
            {
                //Add force to the rigidbody we hit, in the direction it was hit from
                hit.rigidbody.AddForce(-hit.normal * hitForce);
            }
        }
        else
        {
            //if we did not hit anything, set the end of the line to a position directly away from
            laserLine.SetPosition(1, fpsCam.transform.forward * weaponRange);
        }
    }
コード例 #15
0
    void Fire()
    {
        currentAmmo--;
        SetAmmoText();
        gunAudio.Play();
        Vector3    rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0f));
        RaycastHit hit;

        laserLine.SetPosition(0, gunEnd.position);
        if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
        {
            laserLine.SetPosition(1, hit.point);

            ShootableBox health = hit.collider.GetComponent <ShootableBox>();


            if (health != null)
            {
                Debug.Log("DAMAGE!!");
                Instantiate(bloodEffect, hit.point, transform.rotation);
                health.Damage(gunDamage);
            }
            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * hitForce);
            }

            BossShootableBox bossHealth = hit.collider.GetComponent <BossShootableBox>();

            if (bossHealth != null)
            {
                Debug.Log("DAMAGE!!");
                Instantiate(bloodEffect, hit.point, transform.rotation);
                bossHealth.Damage(gunDamage);
            }
            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * hitForce);
            }
        }
        else
        {
            laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
        }
    }
コード例 #16
0
ファイル: RaycastShoot.cs プロジェクト: ks-cap/Raycast-prac
    void Update()
    {
        // Fireボタンが押され, かつ十分な時間が経過すれば発砲する
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;

            StartCoroutine(ShotEffect());
            // 光線の原点(画面の中心)
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
            // 光線がオブジェクトに当たったとき返される情報
            RaycastHit hit;
            // レーザーラインの開始位置と終了位置
            laserline.SetPosition(0, gunEnd.position);

            // 何かに当たった際(光線の初期位置,光線を投射する方向, out, 範囲)
            // out:戻り値の値に加えて関数からの追加情報も取得
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                // 何かを当てた場合
                laserline.SetPosition(1, hit.point);

                ShootableBox health = hit.collider.GetComponent <ShootableBox>();

                if (health != null)
                {
                    health.Damage(gunDamage);
                }

                // レイキャストでヒットしたら
                if (hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(hit.normal * hitForce);
                }
            }
            else
            {
                // 原点から50ユニット離れている場合(範囲外)
                laserline.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
コード例 #17
0
    void Update()
    {
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire) //Checks for fire button and time from shot
        {
            nextFire = Time.time + fireRate;                      // Update time when  player can fire next


            StartCoroutine(ShotEffect()); // Starts ShotEffect coroutine to turn our laser line on/off

            // Create a vector at the center of the cameras viewport
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));


            RaycastHit hit; // Raycast stores information what it has hit


            laserLine.SetPosition(0, gunEnd.position); // Set start position for visual effect for laser to the position of gunEnd


            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange)) // Check if our raycast has hit anything
            {
                laserLine.SetPosition(1, hit.point);                                        // Set the end position for our laser line

                ShootableBox health = hit.collider.GetComponent <ShootableBox>();           // Get a reference to a health script attached to the collider we hit

                if (health != null)
                {
                    health.Damage(gunDamage);
                }

                if (hit.rigidbody != null)                          // Check if the object player hit has a rigidbody attached
                {
                    hit.rigidbody.AddForce(-hit.normal * hitForce); // Add force to the rigidbody we hit, in the direction from which it was hit
                }
            }
            else
            {
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange)); //set the end of the line to a position directly in front of the camera at the distance of weaponRange
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time > m_nextFire)
        {
            m_nextFire = Time.time + m_fireRate;

            StartCoroutine(ShotEffect());

            // origin point for the ray
            // .ViewportToWorldPoint() makes sure that the raycast is ALWAYS at the center of the screen/ first person camera view.
            Vector3 rayOrigin = m_fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

            // holds the info from the ray if it hits a game object w/ collider.
            RaycastHit hit;

            // start & end positions of laser line when player fires.
            m_laserLine.SetPosition(0, m_gunEnd.position); // gives current position in worldspace of the gun object.

            // Physics.Raycast() returns a bool "true" I.I.F. it hits something.
            if (Physics.Raycast(rayOrigin, m_fpsCam.transform.forward, out hit, m_weaponRange))
            {
                m_laserLine.SetPosition(1, hit.point);

                ShootableBox health = hit.collider.GetComponent <ShootableBox>();
                if (health)
                {
                    health.Damage(m_gunDamage);
                }

                if (hit.rigidbody)
                {
                    hit.rigidbody.AddForce(-hit.normal * m_hitForce);
                }
            }
            else
            {
                m_laserLine.SetPosition(0, rayOrigin + (m_fpsCam.transform.forward * m_weaponRange));
            }
        }
    }
コード例 #19
0
    void Update()
    {
        // If left mouse button is clicked
        // and time since last shot is higher than cooldown
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            // Set cooldown for next shot
            nextFire = Time.time + fireRate;

            // Set rayOrigin to middle point(0.5f, 0.5f) of Camera
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0f));

            RaycastHit  hit;
            BoxCollider box;
            box = GetComponent <BoxCollider>();

            laserLine.SetPosition(0, gunEnd.position);

            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                laserLine.SetPosition(1, hit.point);
                ShootableBox health = hit.collider.GetComponent <ShootableBox>();

                if (health != null)
                {
                    health.Damage(gunDamage);
                }

                if (hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                laserLine.SetPosition(1, fpsCam.transform.forward * weaponRange);
            }
        }
    }
コード例 #20
0
ファイル: RaycastShoot.cs プロジェクト: Bhasura/Ice-n-Spice
    public void ShootObj()
    {
        // Set the end position for our laser line
        laserLine.SetPosition(1, hit.point);

        // Get a reference to a health script attached to the collider we hit
        ShootableBox health = hit.collider.GetComponent <ShootableBox>();

        // If there was a health script attached
        if (health != null)
        {
            // Call the damage function of that script, passing in our gunDamage variable
            health.Damage(gunDamage);
        }

        // Check if the object we hit has a rigidbody attached
        if (hit.rigidbody != null)
        {
            // Add force to the rigidbody we hit, in the direction from which it was hit
            hit.rigidbody.AddForce(-hit.normal * hitForce);
        }
    }
コード例 #21
0
    // Update is called once per frame
    void Update()
    {
        //The following code will check if the value for game time
        //has exceeded the nextFireTime. if so, they can fire again
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;

            StartCoroutine(ShotEffect_cr());

            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(.5f, .5f, 0));

            RaycastHit hit;
            laserLine.SetPosition(0, gunEnd.position);
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                laserLine.SetPosition(1, hit.point);

                ShootableBox health = hit.collider.GetComponent <ShootableBox>();

                if (health != null)
                {
                    health.Damage(gunDamage);
                }

                if (hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                laserLine.SetPosition(1, fpsCam.transform.forward * weaponRange);
            }
        }
    }
コード例 #22
0
    void Update()
    {
        Physics.gravity = new Vector3(0, 0.1f, 0);

        //for (var touch : Touch in Input.touches)
        for (int i = 0; i < Input.touchCount; ++i)
        {
            // Check if the player has pressed the fire button and if enough time has elapsed since they last fired
            if ((Input.GetTouch(i).phase == TouchPhase.Began || Input.GetButtonDown("Fire1")) && Time.time > nextFire)
            {
                // Update the time when our player can fire next
                nextFire = Time.time + fireRate;

                // Start our ShotEffect coroutine to turn our laser line on and off
                StartCoroutine(ShotEffect());

                // Create a vector at the center of our camera's viewport
                //Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0.0f));
                var rayOrigin = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);

                // Declare a raycast hit to store information about what our raycast has hit
                RaycastHit hit;


                // Set the start position for our visual effect for our laser to the position of gunEnd
                laserLine.SetPosition(0, gunEnd.position);

                // Check if our raycast has hit anything

                if (Physics.Raycast(rayOrigin, out hit, weaponRange))
                {
                    // Set the end position for our laser line
                    laserLine.SetPosition(1, hit.point);


                    // Get a reference to a health script attached to the collider we hit
                    ShootableBox health = hit.collider.GetComponent <ShootableBox>();


                    // If there was a health script attached
                    if (health != null)
                    {
                        // Call the damage function of that script, passing in our gunDamage variable
                        lifetime = health.Damage(gunDamage, lifetime, score);
                        //countdown = true;
                    }


                    // Check if the object we hit has a rigidbody attached
                    if (hit.rigidbody != null)
                    {
                        // Add force to the rigidbody we hit, in the direction from which it was hit
                        hit.rigidbody.AddForce(-hit.normal * hitForce);
                        //hit.rigidbody.velocity = hit.rigidbody.velocity*2;
                        hit.rigidbody.useGravity = false;

                        //transform.Translate(-hit.normal * movementSpeed * Time.deltaTime);
                        //hit.rigidbody.useGravity = true;
                        //transform.Translate(
                    }
                }
                else
                {
                    // If we did not hit anything, set the end of the line to a position directly in front of the camera at the distance of weaponRange
                    //laserLine.SetPosition (1, rayOrigin + (fpsCam.transform.forward * weaponRange));
                }
            }
        }
    }
コード例 #23
0
    private float nextFire;                                                     // Float to store the time the player will be allowed to fire again, after firing

    void Update()
    {
        // Check if the player has pressed the fire button and if enough time has elapsed since they last fired
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            string colorUsar = numToString(ColorActivo);
            // Update the time when our player can fire next
            nextFire = Time.time + fireRate;

            // Start our ShotEffect coroutine to turn our laser line on and off
            StartCoroutine(ShotEffect());

            // Create a vector at the center of our camera's viewport
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

            // Set the start position for our visual effect for our laser to the position of gunEnd
            laserLine.SetPosition(0, gunEnd.position);

            RaycastHit[] hits;
            hits = Physics.RaycastAll(rayOrigin, fpsCam.transform.forward, weaponRange);

            if (hits.Length > 0)
            {
                //ordenar el array
                RaycastHit temp;
                for (int write = 0; write < hits.Length; write++)
                {
                    for (int sort = 0; sort < hits.Length - 1; sort++)
                    {
                        if (hits[sort].distance > hits[sort + 1].distance)
                        {
                            temp           = hits[sort + 1];
                            hits[sort + 1] = hits[sort];
                            hits[sort]     = temp;
                        }
                    }
                }

                for (int i = 0; i < hits.Length; i++)
                {
                    RaycastHit   hit = hits[i];
                    ShootableBox sB  = hit.collider.GetComponent <ShootableBox>();

                    if (sB)
                    {
                        Debug.Log(colorUsar);
                        laserLine.SetPosition(1, hit.point);

                        sB.Damage(colorUsar);
                        return;
                    }

                    DonutDeColor dC = hit.collider.GetComponent <DonutDeColor>();
                    if (dC)
                    {
                        if ((colorUsar == "Amarillo" && dC.Color == "Azul") || (colorUsar == "Azul" && dC.Color == "Amarillo"))
                        {
                            colorUsar = "Verde";
                            Debug.Log(colorUsar);
                        }
                        else if ((colorUsar == "Rojo" && dC.Color == "Azul") || (colorUsar == "Azul" && dC.Color == "Rojo"))
                        {
                            colorUsar = "Morado";
                        }
                        else if ((colorUsar == "Amarillo" && dC.Color == "Rojo") || (colorUsar == "Rojo" && dC.Color == "Amarillo"))
                        {
                            colorUsar = "Naranja";
                        }
                        laserLine.SetPosition(1, hit.point);
                    }
                    else
                    {
                        laserLine.SetPosition(1, hit.point);
                    }
                }
            }
            else
            {
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
        else if (Input.GetButtonDown("Fire2"))
        {
            ColorActivo = (ColorActivo + 1) % 3;
        }
    }
コード例 #24
0
    void Update()
    {
        point.color         = ColorList[Current];
        colorSelector.color = ColorList[Current];
        NextColor.color     = ColorList[Next];
        PreviousColor.color = ColorList[Previous];

        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;

            StartCoroutine(ShotEffect());

            Vector3    rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
            RaycastHit hit;

            laserLine.SetPosition(0, gunEnd.position);

            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                laserLine.SetPosition(1, hit.point);
                ShootableBox shootableBox = hit.collider.GetComponent <ShootableBox> ();

                if (shootableBox != null)
                {
                    if (shootableBox.isColor(ColorList[Current]))
                    {
                        shootableBox.Damage(gunDamage);
                        //shootableBox.Colored(ColorList[Current]);
                    }
                    shootableBox.Colored(ColorList[Current]);
                }

                if (hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }

        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            Current  = Current + 1;
            Previous = Previous + 1;
            Next     = Next + 1;

            if (Current >= 3)
            {
                Current = 0;
            }
            if (Previous >= 3)
            {
                Previous = 0;
            }
            if (Next >= 3)
            {
                Next = 0;
            }
        }

        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            Current  = Current - 1;
            Previous = Previous - 1;
            Next     = Next - 1;

            if (Current <= -1)
            {
                Current = 2;
            }
            if (Previous <= -1)
            {
                Previous = 2;
            }
            if (Next <= -1)
            {
                Next = 2;
            }
        }
    }
コード例 #25
0
    void Update()
    {
        // Check if the player has pressed the fire button and if enough time has elapsed since they last fired
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            // Update the time when our player can fire next
            nextFire = Time.time + fireRate;

            // Start our ShotEffect coroutine to turn our laser line on and off
            StartCoroutine(ShotEffect());

            // Create a vector at the center of our camera's viewport
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

            // Declare a raycast hit to store information about what our raycast has hit
            RaycastHit hit;


            // Set the start position for our visual effect for our laser to the position of gunEnd
            laserLine.SetPosition(0, gunEnd.position);

            // Check if our raycast has hit anything
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                // if (Physics.SphereCast(rayOrigin, 100,fpsCam.transform.forward, out hit, weaponRange))

                // Set the end position for our laser line
                laserLine.SetPosition(1, hit.point);

                // Get a reference to a health script attached to the collider we hit
                ShootableBox     health = hit.collider.GetComponent <ShootableBox>();
                MovePlatform     mp     = hit.collider.GetComponent <MovePlatform>();
                MoveablePlatform mc     = hit.collider.GetComponent <MoveablePlatform>();
                MultiMove        mm     = hit.collider.GetComponent <MultiMove>();

                if (health != null)
                {
                    health.Damage(gunDamage);
                }

                if (mp != null)
                {
                    //use if MoveablePlatform is mine not prefab
                    mp.Frozen();
                }

                if (mm != null)
                {
                    //use if MoveablePlatform is mine not prefab
                    mm.Frize();
                }
                if (mc != null)
                {
                    //use if MoveablePlatform is mine not prefab
                    mc.Froze();
                }

                // Check if the object we hit has a rigidbody attached
                if (hit.rigidbody != null)
                {
                }
            }
            else
            {
                // If we did not hit anything, set the end of the line to a position directly in front of the camera at the distance of weaponRange
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
コード例 #26
0
    void Update()
    {
        // Check if the player has pressed the fire button and if enough time has elapsed since they last fired
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire && counter < LimitShot)
        {
            // Update the time when our player can fire next
            nextFire = Time.time + fireRate;
            counter++;

            // Start our ShotEffect coroutine to turn our laser line on and off
            StartCoroutine(ShotEffect());

            // Create a vector at the center of our camera's viewport
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

            // Declare a raycast hit to store information about what our raycast has hit
            RaycastHit hit;

            // Set the start position for our visual effect for our laser to the position of gunEnd
            laserLine.SetPosition(0, gunEnd.position);

            // Check if our raycast has hit anything
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                // Set the end position for our laser line
                laserLine.SetPosition(1, hit.point);

                // Get a reference to a health script attached to the collider we hit

                //target 1
                ShootableBox health  = hit.collider.GetComponent <ShootableBox>();
                twoPoints    health2 = hit.collider.GetComponent <twoPoints>();
                threePoints  health3 = hit.collider.GetComponent <threePoints>();
                fourPoints   health4 = hit.collider.GetComponent <fourPoints>();
                fivePoints   health5 = hit.collider.GetComponent <fivePoints>();

                //missed bullet
                Missed health6 = hit.collider.GetComponent <Missed>();

                //target 2
                Target2PointOne   target2_point1 = hit.collider.GetComponent <Target2PointOne>();
                Target2PointTwo   target2_point2 = hit.collider.GetComponent <Target2PointTwo>();
                Target2PointThree target2_point3 = hit.collider.GetComponent <Target2PointThree>();
                Target2Point4     target2_point4 = hit.collider.GetComponent <Target2Point4>();
                Target2Point5     target2_point5 = hit.collider.GetComponent <Target2Point5>();
                //target 3
                Target3Point1 target3_point1 = hit.collider.GetComponent <Target3Point1>();
                Target3Point2 target3_point2 = hit.collider.GetComponent <Target3Point2>();
                Target3Point3 target3_point3 = hit.collider.GetComponent <Target3Point3>();
                Target3Point4 target3_point4 = hit.collider.GetComponent <Target3Point4>();
                Target3Point5 target3_point5 = hit.collider.GetComponent <Target3Point5>();

                //target 1
                // If there was a health script attached
                if (health != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    health.Damage(gunDamage);
                }
                // If there was a health script attached
                if (health2 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    health2.Damage(gunDamage);
                }
                // If there was a health script attached
                if (health3 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    health3.Damage(gunDamage);
                }
                // If there was a health script attached
                if (health4 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    health4.Damage(gunDamage);
                }
                // If there was a health script attached
                if (health5 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    health5.Damage(gunDamage);
                }

                //missed target
                // If there was a health script attached
                if (health6 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    health6.Damage(gunDamage);
                }


                // target 2
                // If there was a health script attached
                if (target2_point1 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target2_point1.Damage(gunDamage);
                }

                // If there was a health script attached
                if (target2_point2 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target2_point2.Damage(gunDamage);
                }

                // If there was a health script attached
                if (target2_point3 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target2_point3.Damage(gunDamage);
                }

                // If there was a health script attached
                if (target2_point4 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target2_point4.Damage(gunDamage);
                }

                // If there was a health script attached
                if (target2_point5 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target2_point5.Damage(gunDamage);
                }

                //target 3
                // If there was a health script attached
                if (target3_point1 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target3_point1.Damage(gunDamage);
                }

                // If there was a health script attached
                if (target3_point2 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target3_point2.Damage(gunDamage);
                }

                // If there was a health script attached
                if (target3_point3 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target3_point3.Damage(gunDamage);
                }

                // If there was a health script attached
                if (target3_point4 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target3_point4.Damage(gunDamage);
                }

                // If there was a health script attached
                if (target3_point5 != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    target3_point5.Damage(gunDamage);
                }
                // Check if the object we hit has a rigidbody attached
                if (hit.rigidbody != null)
                {
                    // Add force to the rigidbody we hit, in the direction from which it was hit
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                // If we did not hit anything, set the end of the line to a position directly in front of the camera at the distance of weaponRange
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }