如何使鼠标右键单击自动拍摄,如果再次单击鼠标左键,则每次单击鼠标都会拍摄一次?

时间:2019-04-20 18:53:01

标签: c# unity3d

我希望能够在单次单击的鼠标左键之间切换,并且一旦单击鼠标右键将自动停止拍摄,直到再次单击鼠标左键。而且autoFireBool变量没有用,因为我是在构建不在编辑器中的游戏之后谈论的。

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

public class Shooting : MonoBehaviour
{
    [SerializeField]
    private Transform[] firePoints;
    [SerializeField]
    private Rigidbody projectilePrefab;
    [SerializeField]
    private float launchForce = 700f;
    [SerializeField]
    private Animator anim;
    [SerializeField]
    private bool automaticFire = false;
    [SerializeField]
    private bool slowDownEffect = false;

    private void Start()
    {
        anim.SetBool("Shooting", true);
    }

    public void Update()
    {
        if (isAnimationStatePlaying(anim, 0, "AIMING") == true)
        {
            if (Input.GetButtonDown("Fire1") && automaticFire == false)
            {
                if (anim.GetBool("Shooting") == true)
                {
                    anim.Play("SHOOTING");
                    LaunchProjectile();
                }
            }
            else
            {
                if (/*automaticFire == true &&*/Input.GetButtonDown("Fire2"))
                {
                    anim.Play("SHOOTING");
                    LaunchProjectile();
                }
            }
        }
    }

    private void LaunchProjectile()
    {
        foreach (var firePoint in firePoints)
        {
            Rigidbody projectileInstance = Instantiate(
                projectilePrefab,
                firePoint.position,
                firePoint.rotation);

            projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);

            projectileInstance.gameObject.AddComponent<BulletDestruction>().Init();
        }
    }

    bool isAnimationStatePlaying(Animator anim, int animLayer, string stateName)
    {
        if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName))
            return true;
        else
            return false;
    }
}

我尝试过:

if (/*automaticFire == true &&*/Input.GetButtonDown("Fire2"))
                {
                    anim.Play("SHOOTING");
                    LaunchProjectile();
                }

但是,每次单击鼠标右键都会拍摄单张照片。 而且我希望单击鼠标右键会自动不停地拍摄,而单击鼠标左键将切换为单张照片。

2 个答案:

答案 0 :(得分:0)

您可以尝试执行以下操作:

class Foo : MonoBehaviour 
{
    [SerializableField]
    private bool shooting = false;
    [SerializableField]
    private Animator anim;

    public void Update() 
    {
        if (shooting)
        {
            //if the shooting variable is set to true on the if below, it will start shooting
            anim.Play("Shooting");
            LaunchProjectile();
        }

        if (!shooting && Input.GetButtonDown("Fire1"))
        {
            anim.Play("Shooting");
            LaunchProjectile();
        }
        else if (Input.GetButtonDown("Fire2"))
        {
            //reversing the shooting variable
            shooting = !shooting;
            if (shooting)
            {
                //this is just to make sure it will shoot right away
                //and not on the next frame
                anim.Play("Shooting");
                LaunchProjectile();
            }
        }
    }
}

答案 1 :(得分:0)

您可以使用标志,因此当您单击鼠标右键然后将标志设置为true时,可以播放动画并发射弹丸。

更改的代码是

\documentclass{report}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\section{Introduction}
Here are the normal paragraph crossing colmuns.

\blindtext

\begin{minipage}[t]{0.5\textwidth}
  \section{Argument on the Left}
  Because I am on the left, so must I be not right?

\end{minipage}\begin{minipage}[t]{0.5\textwidth}
  \section{Argument on the Right}
  Because I am on the right, so I must be right!

  \blindtext
\end{minipage}
\end{document}

如果这有助于考虑投票并接受答案。