【问题标题】:Guns fire automatically on startup?枪开机自动开火?
【发布时间】:2019-02-14 23:07:46
【问题描述】:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class PlayerController : MonoBehaviour
{
    [Header("General")]
    [Tooltip("In ms^-1")] [SerializeField] float controlSpeed = 24f;
    [Tooltip("In m")] [SerializeField] float xRange = 6f;
    [Tooltip("In m")] [SerializeField] float yRange = 4f;
    [SerializeField] GameObject[] guns;

    [Header("Screen-position Based")]
    [SerializeField] float positionPitchFactor = -5f;
    [SerializeField] float positionYawFactor = 5f;

    [Header("Control-throw Based")]
    [SerializeField] float controlPitchFactor = -20f;
    [SerializeField] float controlRollFactor = -20f;

    float xThrow, yThrow;
    bool isControlEnabled = true;
    private bool landed = true;
    private bool fired = true;
    private GameController gameController;


    private void Start()
    {
        gameController = FindObjectOfType<GameController>();
    }

    // Update is called once per frame
    void Update()
    {
        if (isControlEnabled)
        {
            ProcessTranslation();
            ProcessRotation();
            ProcessFiring();
        }

    }
    void OnPlayerDeath()
    {
        isControlEnabled = false;
        gameController.GameOver();
    }
    private void ProcessRotation()
    {
        float pitchDueToPosition = transform.localPosition.y *     positionPitchFactor;
        float pitchDueToControlThrow = yThrow * controlPitchFactor;
        float pitch = pitchDueToPosition + pitchDueToControlThrow;
        float yaw = transform.localPosition.x * positionYawFactor;
        float roll = xThrow * controlRollFactor;

        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
    }

    private void ProcessTranslation()
    {

        xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        yThrow = CrossPlatformInputManager.GetAxis("Vertical");

        float xOffset = xThrow * controlSpeed * Time.deltaTime;
        float yOffset = yThrow * controlSpeed * Time.deltaTime;


        float rawXPos = transform.localPosition.x + xOffset;
        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

        float rawYPos = transform.localPosition.y + yOffset;
        float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

        transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
    }
    void ProcessFiring()
    {
        if (CrossPlatformInputManager.GetButton("Fire"))
        {
            SetGunsActive(true);
        }

        else
        {
            SetGunsActive(false);
        }

    }

    private void SetGunsActive(bool isActive)
    {
        foreach (GameObject gun in guns) // care may affect death FX
        {
            var emissionModule = gun.GetComponent<ParticleSystem>().emission;
            emissionModule.enabled = isActive;
        }
    }

    void FireGunsOn()
    {
        isControlEnabled = false;

        if (!fired)
        {
            gameController.GameOver();
        }
    }
    void FireGunsOff()
    {
        isControlEnabled = true;
        fired = false;
    }
    }

    void OnPlayerLanding()
    {
        isControlEnabled = false;

        if (!landed)
        {
            gameController.GameOver();
        }
    }

    void OnPlayerTakeOff()
    {
        isControlEnabled = true;
        landed = false;

    }

我正在尝试让枪支在启动时自动开火。当我开始游戏时,激光枪开始射击,而我没有按火/空格键。我添加了 void fireGunsOn 和 fireGunsOff 方法,但我收到 void OnPlayerLanding() 的语法错误,说明:Assets/Scripts/PlayerController.cs(120,0): error CS1525: Unexpected symbol `void' 有什么见解值得赞赏吗?

【问题讨论】:

  • 您的代码末尾有一个额外的右括号。您可能需要查看会为您突出显示的编辑器中的格式。
  • 这表示您收到语法错误,因为在您的FireGunsOff 方法之后有一个额外的}。这将关闭类 aswel 意味着 OnPlayerLanding 超出范围
  • 好的,我试试这个谢谢。
  • 我删除了多余的括号谢谢我还将 void FireGunsOn() 中的 if 语句更改为 gameController.GameOver();但是枪的问题在启动时继续射击。这些方法在阻止枪支在启动时射击方面是否准确/有用?缺少什么?
  • ProcessFiring()的顶部放一条调试语句,看看它是否打印到控制台。

标签: c# unity3d


【解决方案1】:

你只是有一个不属于那里的“}”:

void FireGunsOff()
{
    isControlEnabled = true;
    fired = false;
}
}

删除第二个并将其放在课程的末尾

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 2021-06-26
    • 2011-06-02
    相关资源
    最近更新 更多