Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使鼠标右键单击自动射击,如果再次单击鼠标左键,每次鼠标左键单击都将射击?_C#_Unity3d - Fatal编程技术网

C# 如何使鼠标右键单击自动射击,如果再次单击鼠标左键,每次鼠标左键单击都将射击?

C# 如何使鼠标右键单击自动射击,如果再次单击鼠标左键,每次鼠标左键单击都将射击?,c#,unity3d,C#,Unity3d,我想能够切换之间的鼠标左键点击单杆和一旦点击鼠标右键将拍摄自动无停止,直到再次点击鼠标左键。automaticFire bool变量没有任何用处,因为我说的是在构建游戏之后,而不是在编辑器中 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shooting : MonoBehaviour { [SerializeField]

我想能够切换之间的鼠标左键点击单杆和一旦点击鼠标右键将拍摄自动无停止,直到再次点击鼠标左键。automaticFire bool变量没有任何用处,因为我说的是在构建游戏之后,而不是在编辑器中

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;
    }
}
但每次单击鼠标右键时,都会进行单次拍摄。
我希望鼠标右键一键将自动停止,左键一键将切换为单发。

您可以尝试这样做:

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();
            }
        }
    }
}

您可以尝试这样做:

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();
            }
        }
    }
}

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

更改的代码是

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(Input.GetButtonDown("Fire1") && automaticFire == true){
              automaticFire = true;
            }
            else
            {
                if (/*automaticFire == true &&*/Input.GetButtonDown("Fire2"))
                {
                    automaticFire = true;
                }
                if (automaticFire == true )
                {
                    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;
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公开课射击:单一行为
{
[序列化字段]
私人火力点;
[序列化字段]
私人刚体投影;
[序列化字段]
私人浮动发射力=700f;
[序列化字段]
私人动画师;
[序列化字段]
私有布尔自动点火=假;
[序列化字段]
私有布尔慢降效应=假;
私有void Start()
{
动画设定工具(“射击”,真实);
}
公共无效更新()
{
如果(isAnimationStatePlaying(动画,0,“瞄准”)==true)
{
if(Input.GetButtonDown(“Fire1”)和&automaticFire==false)
{
if(anim.GetBool(“拍摄”)==true)
{
动漫游戏(“射击”);
发射弹();
}
}else if(Input.GetButtonDown(“Fire1”)和&automaticFire==true){
自动点火=真;
}
其他的
{
如果(/*automaticFire==true&&*/Input.GetButtonDown(“Fire2”))
{
自动点火=真;
}
如果(automaticFire==真)
{
动漫游戏(“射击”);
发射弹();
}
}
}
}
私有无效发射弹()
{
foreach(火力点中的var火力点)
{
刚体投影实例=实例化(
ProjectlePrefab,
位置,
火点旋转);
ProjectleInstance.AddForce(新矢量3(0,0,1)*启动力);
projectleInstance.gameObject.AddComponent().Init();
}
}
bool isAnimationStatePlaying(动画师anim、int animLayer、字符串stateName)
{
if(anim.GetCurrentAnimatorStateInfo(animLayer.IsName(stateName))
返回true;
其他的
返回false;
}
}

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

你可以使用标志,所以当你点击鼠标右键,然后设置一个标志为真,并播放动画和射击弹丸。< /P> 更改的代码是

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(Input.GetButtonDown("Fire1") && automaticFire == true){
              automaticFire = true;
            }
            else
            {
                if (/*automaticFire == true &&*/Input.GetButtonDown("Fire2"))
                {
                    automaticFire = true;
                }
                if (automaticFire == true )
                {
                    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;
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公开课射击:单一行为
{
[序列化字段]
私人火力点;
[序列化字段]
私人刚体投影;
[序列化字段]
私人浮动发射力=700f;
[序列化字段]
私人动画师;
[序列化字段]
私有布尔自动点火=假;
[序列化字段]
私有布尔慢降效应=假;
私有void Start()
{
动画设定工具(“射击”,真实);
}
公共无效更新()
{
如果(isAnimationStatePlaying(动画,0,“瞄准”)==true)
{
if(Input.GetButtonDown(“Fire1”)和&automaticFire==false)
{
if(anim.GetBool(“拍摄”)==true)
{
动漫游戏(“射击”);
发射弹();
}
}else if(Input.GetButtonDown(“Fire1”)和&automaticFire==true){
自动点火=真;
}
其他的
{
如果(/*automaticFire==true&&*/Input.GetButtonDown(“Fire2”))
{
自动点火=真;
}
如果(automaticFire==真)
{
动漫游戏(“射击”);
发射弹();
}
}
}
}
私有无效发射弹()
{
foreach(火力点中的var火力点)
{
刚体投影实例=实例化(
ProjectlePrefab,
位置,
火点旋转);
ProjectleInstance.AddForce(新矢量3(0,0,1)*启动力);
projectleInstance.gameObject.AddComponent().Init();
}
}
bool isAnimationStatePlaying(动画师anim、int animLayer、字符串stateName)
{
if(anim.GetCurrentAnimatorStateInfo(animLayer.IsName(stateName))
返回true;
其他的
返回false;
}
}

如果这有助于考虑一个赞成和接受的答案。

首先我在这里面检查自动火灾到false,或者它不工作如果它是真的:否则(输入。GETButOnDon(“Fiel1”)& AutoDealFix==TRUE){AutoFixFix= false;}第二个问题当它处于自动射击状态时,我需要多次单击鼠标左键,直到它变回单发射击。我想问题是我正在等待动画在这一行结束:如果(isAnimationStatePlaying(anim,0,“AIMING”)==true),并且从那以后,当它处于自动状态时,鼠标左键一直不起作用。我必须在动画