I remember from a tutorial that some mouse buttons have different levels with which they can be pushed. How do I code in C# the mouse button to work no matter how soft or hard it is pushed?
If you use the OnMouseDown() event your code will execute as soon as the mouse button is pressed on the object that you attach the script to. Here is an example:
void OnMouseDown(){
//Put your code to execute here.
}
It’s been a while since I’ve used it, but I think you have to have a collider attached to the gameobject as well.
Hope that’s what you’re looking for
Unity can handle two sorts of input:
- Key/Button inputs are discrete and binary, so they’re either on or off
- Axis inputs are continuous and analog, so they can have a range of values
Generally speaking, Unity handles mouse button presses using the former. I don’t doubt that some mice have options beyond the standard, but Unity is built toward the common case: a mouse button is either pressed or it isn’t.
For more information, head on over to the game input manual page, and then check out the Input class documentation to see how it’s used in your scripts.
Basic example in JS:
function Update() {
if (Input.GetMouseButtonDown(0)) {
//true for ONE frame when left mouse button is first clicked
}
if (Input.GetMouseButton(0)) {
//true for EACH frame while left mouse button is held down
}
}