I'm a very beginner coder so please forgive how rudimentary this code is. I'm trying to make a simple game where you accrue points while the mouse is down with a multiplier for mouse speed. Currently my mouse speed function is not working correctly, it seems to be a fixed variable at the moment. If anybody could point out what I've done wrong I'd be very appreciative. Sorry if there is already an answer for this, I searched the archives and didn't find anything that quite answered my question.
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;
}
}
}
It seems that you are checking upon if the primary mouse button is pressed twice. From both
if (Input.GetMouseButtonDown(0))
and
else if (Input.GetMouseButton(0))
Furthermore you will have to also update the last mouse position through each iteration, where as of now you do it only when the mouse button is pressed, which is not incorrect but you will have to do it when the button is not pressed aswell.
I do believe your code could be fixed if you changed it to the following:
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;
}
}
This does not solve your issue with distributing a score based on the distance moved, but you should be able to implement this by using mouseDelta.
Thanks for your advice everyone. Here is the final working code for anyone who may need it in the future. Adjust the values for the speed multipliers if you desire, as well as how often the fixed update is called.
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;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.