枪在启动时会自动射击吗?

时间:2019-02-14 14:08:11

标签: c# unity3d

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:意外符号'void' 任何见解是赞赏?

1 个答案:

答案 0 :(得分:0)

您只是有一个不属于此的“}”:

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

删除第二个,并将其放到课程末尾