c# - Unity如何让我的枪在玩家移动时停止射击?

标签 c# unity-game-engine

enter image description here我正在制作一个枪支射击脚本,但我不知道如何在玩家奔跑时禁用射击。有人可以帮我吗?

void Shoot()
{
    MuzzleFlash.Play();


    RaycastHit hit;
    if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, Range))
    {
        Debug.Log(hit.transform.name);

        EnemyHealth enemy = hit.transform.GetComponent<EnemyHealth>();
        if (enemy != null)
        {
            enemy.TakeDamage(Damage);
        }

    }
}

这是我的角色 Controller 脚本的运动部分:

void Movement()
{
    transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * WalkSpeed * Time.deltaTime);
    transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * WalkSpeed * Time.deltaTime);

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        WalkSpeed = RunSpeed;
    }
    if (Input.GetKeyUp(KeyCode.LeftShift))
    {
        WalkSpeed = DefaultSpeed;
    }
}

最佳答案

您可以在其他脚本中设置变量,如下所示:

[Serialize Field]
private GameObject objectWithYourScriptYouNeed; 
private ClassOfScriptYouNeed pS; 


void Start()
{     
    pS = objectWithYourScript.GetComponent<ClassOfScriptYouNeed>();    
    pS.varName = 12345;
}

所以,在你的拍摄脚本中:

Public Bool isMoving;

void Shoot()
{
    //I'm assuming you also dont want muzzleflash to play.
    if (isMoving != true)
    {
        MuzzleFlash.Play();    

        RaycastHit hit;
        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, Range))
        {

            Debug.Log(hit.transform.name);

            EnemyHealth enemy = hit.transform.GetComponent<EnemyHealth>();
            if (enemy != null)
            {
                enemy.TakeDamage(Damage);
            }
        }
    }
}

你的 ActionScript :

//put your shooting scripts name below.
private ShootScript shootScript; 

void start()
{    

    shootScript = GetComponent<ShootScript>();

}

void Movement()
{
    shootScript.isMoving = Input.GetAxis("Vertical") != 0f 
                           || Input.GetAxis("Horizontal") != 0f ; 

    transform.Translate(Vector3.forward * Input.GetAxis("Vertical") 
                        * WalkSpeed * Time.deltaTime);
    transform.Translate(Vector3.right * Input.GetAxis("Horizontal") 
                        * WalkSpeed * Time.deltaTime);

    // GetKey is true on every frame that shift is held down, and false when it isn't
    // GetKeyDown is only true on the first frame in a row that shift is held down    
    if (Input.GetKey(KeyCode.LeftShift))
    {
        WalkSpeed = RunSpeed;
    }
    else 
    {
        WalkSpeed = DefaultSpeed;
    }
}

关于c# - Unity如何让我的枪在玩家移动时停止射击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57225665/

相关文章:

c# - 获取类型名称

c# - Parallel.For() 和 Windows 消息循环

c# - 在可移植类库中使用 Json

android - Google Cardboard for Unity 只渲染一个光源

android - FireBase Cloud Messaging - 如何从 Unity 中的通知打开链接而不是 Activity

android - AdMob。插屏广告播放时能否保持屏幕常亮?

c# - Unity 3D VR (Unity 5.6) 如何使用c#脚本添加事件触发器

c# - WCF + Windows Phone 7

c# - 如何从数组段中获取字节数组

c# - Unity UI 文本框动态高度。如何使其根据文本数量扩展?