Проблемы с GetMouseButtonDown C # - PullRequest
       1

Проблемы с GetMouseButtonDown C #

1 голос
/ 20 сентября 2019
public class PlayerController : MonoBehaviour
{
    public float moveSpeed;
    private Rigidbody myRigidBody;

    private Vector3 moveInput;
    private Vector3 moveVelocity;

    private Camera mainCamera;
    public GunController theGun;
    // Start is called before the first frame update
    void Start()
    {
        myRigidBody = GetComponent<Rigidbody>();
        mainCamera = FindObjectOfType<Camera>();
    }

    // Update is called once per frame
    void Update()
    {
        moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
        moveVelocity = moveInput * moveSpeed;

        Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
        Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
        float rayLength;

        if(groundPlane.Raycast(cameraRay,out rayLength))
        {
            Vector3 pointToLook = cameraRay.GetPoint(rayLength);
            Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);

            transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
        }

        if(Input.GetMouseButtonDown(0))
            print ("The Gun should have fired");
        Debug.Log("Pressed Left Mouse Button.");
            theGun.isFiring = true;

        if (Input.GetMouseButtonDown(0))
            print("The gun should have stopped firing");
            theGun.isFiring = false;
    }
    void FixedUpdate()
    {
        myRigidBody.velocity = moveVelocity;
    }
}

Я считаю, что моя проблема начинается с оператора if, if.Кажется, консоль постоянно регистрирует левую кнопку мыши как нажатую.и без оператора отладки, если я нажимаю вниз, всплывающий текст всплывает, но сразу же переходит к следующему отпечатку, даже если я никогда не щелкнул.С отладочным или печатным кодом или без него, ожидаемый эффект никогда не произойдет.Извините, если отформатирован неправильно, очень плохо знаком с этим.

Консоль отладки

1 Ответ

2 голосов
/ 20 сентября 2019

C # не похож на Python ... C # не использует блоки кода путем отступа строк кода.В c # вам нужно открыть блок с { и закрыть его с }.

Так что эта часть не будет работать так, как вы думаете:

if(Input.GetMouseButtonDown(0))
    print ("The Gun should have fired");
    Debug.Log("Pressed Left Mouse Button.");
    theGun.isFiring = true;

// forgot to negate?!
if (Input.GetMouseButtonDown(0))
    print("The gun should have stopped firing");
    theGun.isFiring = false;

Попробуйте

if (Input.GetMouseButtonDown(0) && !theGun.isFiring) {
    print ("The Gun should have fired");
    Debug.Log("Pressed Left Mouse Button.");
    theGun.isFiring = true;
} else if (theGun.isFiring) {
    print("The gun should have stopped firing");
    theGun.isFiring = false;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...