- Home /
OnMouseDown() tracking bad at 3D box collider
Hello, i wanted to make a drag and drop game. Cubes drop from the sky and you have to catch them. I use the OnMouseDown() function. But if i press on the cube, it doesnt stick everytime to my mouse. If you touch it at the bottom or in the middle it triggers everytime, but at the top it happens nothing.
The cube has a constant velocity (5).
Does anyone has an idea?
Code:
void Update() {
if (selected) {
rig.AddForce((cam.ScreenToWorldPoint(Input.mousePosition) - transform.position) * force);
rig.velocity = Vector3.zero;
} else {
if (!once) {
maxSpeed = maxSpeedPreset * gm.gameSpeed;
rig.velocity = new Vector3(Mathf.Clamp(rig.velocity.x, -maxSpeed, maxSpeed), Mathf.Clamp(rig.velocity.y, -maxSpeed, maxSpeed), Mathf.Clamp(rig.velocity.z, -maxSpeed, maxSpeed));
} else {
maxSpeed = maxSpeedPreset;
rig.velocity = new Vector3(Mathf.Clamp(rig.velocity.x, -MaxThrowSpeed, MaxThrowSpeed), Mathf.Clamp(rig.velocity.y, -MaxThrowSpeed, MaxThrowSpeed), Mathf.Clamp(rig.velocity.z, -MaxThrowSpeed, MaxThrowSpeed));
}
}
}
And the MouseDown function:
void OnMouseDown() {
if (tag != "NoInteract") {
selected = true;
once = true;
}
}
Comment
Your answer
Follow this Question
Related Questions
Make A Cube 'Sticky' 4 Answers
Rigidbody velocity disables gravity 1 Answer
Making a cube fall without rigidbody 1 Answer
Modifying Gravity for Local Player 1 Answer