簡體   English   中英

將鼠標速度倍增器加分

[英]Add mouse speed multiplier to score

我是一個非常初學者的編碼人員,所以請原諒這個編碼的基礎。 我正在嘗試制作一個簡單的游戲,在您按下鼠標速度乘數時,您會在鼠標按下時獲得積分。 目前,我的鼠標速度功能無法正常工作,目前看來是固定變量。 如果有人能指出我做錯了什么,我將非常感激。 抱歉,如果已經有了答案,我搜索了檔案,但沒有找到任何可以回答我問題的信息。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UpdateScoreOnMousePress : MonoBehaviour {

    public Text scoreText;
    public int score;

    public Vector3 mouseDelta = Vector3.zero;
    private Vector3 lastPos = Vector3.zero;
    float mouseSpeed;

    float timeToGo = 0.5f;

    //Initialization
    void Start()
    {
        timeToGo = Time.fixedTime;
    }

    void Update()
    {

    }

    void FixedUpdate()
    {
        //Checks if it has been 0.5 seconds since last call
        if (Time.fixedTime > timeToGo)
        {
            //Updates score on mouse down
            scoreText.text = "Score: " + score + (int)mouseSpeed*0.1;

            //Start mousePosition
            if (Input.GetMouseButtonDown(0))
            {
                mouseDelta = Input.mousePosition;
            }

            else if (Input.GetMouseButton(0))
            {
                mouseDelta = Input.mousePosition - lastPos;

                score++;

                //Shows mouse position and magnitude in console
                Debug.Log("delta X : " + mouseDelta.x);
                Debug.Log("delta Y : " + mouseDelta.y);
                Debug.Log("delta distance : " + mouseDelta.magnitude);

                //Updates mousePosition
                lastPos = Input.mousePosition;

                //Updates mouseSpeed
                mouseSpeed = mouseDelta.magnitude / Time.deltaTime;
            }

            //Updates timeToGo
            timeToGo = Time.fixedTime + 0.5f;
        }
    }
}

似乎您正在檢查是否按下了鼠標主鍵兩次。 從兩者

if (Input.GetMouseButtonDown(0))

else if (Input.GetMouseButton(0))

此外,您還必須在每次迭代中更新最后一個鼠標的位置,到目前為止,僅在按下鼠標按鈕時才執行此操作,這是不正確的,但是在未同時按下按鈕時也必須這樣做。

我確實相信,如果將其更改為以下內容,則可以修復您的代碼:

    void FixedUpdate()
    {
        //Checks if it has been 0.5 seconds since last call
        if (Time.fixedTime > timeToGo)
        {
            //Updates score on mouse down
            scoreText.text = "Score: " + score + (int)mouseSpeed*0.1;    

            if (Input.GetMouseButtonDown(0))
            {

                mouseDelta = Input.mousePosition - lastPos;

                score++;

                //Shows mouse position and magnitude in console
                Debug.Log("delta X : " + mouseDelta.x);
                Debug.Log("delta Y : " + mouseDelta.y);
                Debug.Log("delta distance : " + mouseDelta.magnitude);

                //Updates mouseSpeed
                mouseSpeed = mouseDelta.magnitude / Time.deltaTime;
            }
            lastPos = Input.mousePosition;
            //Updates timeToGo
            timeToGo = Time.fixedTime + 0.5f;
        }
    }

這不能解決基於移動距離來分配分數的問題,但是您應該能夠通過使用mouseDelta來實現。

謝謝大家的建議。 這是將來可能需要的任何人的最終工作代碼。 如果需要,請調整速度倍增器的值,以及調用固定更新的頻率。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UpdateScoreWhileMousePressed : MonoBehaviour {

    public Vector3 mouseDelta = Vector3.zero;
    private Vector3 lastPos = Vector3.zero;

    public float mouseSpeed;

    public Text scoretext;
    public int score;

    public float timeToGo;

    // Use this for initialization
    void Start () {

        timeToGo = Time.fixedTime;
        mouseDelta = Input.mousePosition;
        lastPos = Input.mousePosition;

    }

    // Update is called once per frame
    void Update () {



    }

    // Update is called every 0.2 seconds
    private void FixedUpdate()
    {
        if(Time.fixedTime > timeToGo)
        {

            //Update mouseDelta
            mouseDelta = Input.mousePosition - lastPos;

            //Calculate mouseSpeed
            mouseSpeed = mouseDelta.magnitude / Time.deltaTime;

            scoretext.text = "Score: " + score;

            Debug.Log("Speed: " + mouseSpeed);
            Debug.Log("Score: " + score);

            //If the mouse is being pressed the score will increase by 1 every call
            if (Input.GetMouseButton(0))
            {

                if(mouseSpeed <= 1000)
                {
                    score += 1;
                }

                //And receive multipliers for faster speed
                else if(mouseSpeed > 1000 & mouseSpeed < 2000)
                {
                    score += 1 * 2;
                }

                else if(mouseSpeed >= 2000 & mouseSpeed < 4000)
                {
                    score += 1 * 3;
                }

                else if(mouseSpeed >=4000 & mouseSpeed < 8000)
                {
                    score += 1 * 4;
                }

                else if(mouseSpeed >= 8000)
                {
                    score += 1 * 5;
                }

            }

            //Update lastPost
            lastPos = Input.mousePosition;

            //Update timeToGo
            timeToGo = Time.fixedTime + 0.2f;
        }


    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM