我是一个非常入门的编码人员,所以请原谅该编码的基本知识。我正在尝试制作一个简单的游戏,在您按下鼠标速度乘数时,您会在鼠标按下时获得积分。目前,我的鼠标速度功能无法正常工作,目前看来是固定变量。如果有人能指出我做错了什么,我将非常感激。抱歉,如果已经有了答案,我搜索了档案,但没有找到任何能回答我问题的东西。
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;
}
}
}
答案 0 :(得分:0)
似乎您正在检查是否按下了鼠标主键两次。从两者
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来实现。
答案 1 :(得分:0)
感谢大家的建议。这是将来可能需要的任何人的最终工作代码。如果需要,可以调整速度倍增器的值,以及固定更新的调用频率。
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;
}
}
}