- Home /
How to prevent objects from going through each other when using mouse?
I have a mesh that has a box collider and a rigidbody. I use a mouse to move that mesh. I also rotate that mesh with the mouse wheel. Script looks something like this now:
}
function Update () {
var pyoritaKatta = Input.GetAxis("Mouse ScrollWheel");
transform.Rotate(100*pyoritaKatta * kadenKaantymisherkkyys * Time.deltaTime, 0, 0);
var liikutaKattaX = Input.GetAxis("Mouse X");
var liikutaKattaY = Input.GetAxis("Mouse Y");
transform.Translate(liikutaKattaX * kadenLiikutusnopeusX * Time.deltaTime, liikutaKattaY * kadenLiikutusnopeusY * Time.deltaTime, 0,Space.World);
}
When I put a sphere with rigidbody to fall on top of the mesh, I can move the mouse so fast the sphere goes right through the mesh.
The other problem is the mouse controlled mesh can go through walls with colliders.
I can't use Character Controller because it apparently comes with its own collision sphere.
Answer by DaveA · Mar 26, 2012 at 07:20 PM
Using Translate and Rotate will put things where you say, they won't care about collision. You either need to implement OnCollisionEnter on those objects and have them inform this script to stop, or probably better, use the RigidBody functions MovePosition and MoveRotation
Do I still have to use some kind of collision detection to stop the mesh from moving when I'm using Rigidbody's $$anonymous$$ovePosition? Because replacing Translate with those didn't help. It still can get other rigidbodies to move when touching, but it itself won't stop when hitting a wall.
The other problem was with mouse sensitivity. Because if I want to use that, the mouse can move so fast, it just passes the other rigidbody meshes.
Try implementing OnCollisionEnter and have it access your 'move' script or Send$$anonymous$$essage to tell it to stop moving.
I have tried to get this to work but there's no hope. I use:
function OnCollisionEnter(collision : Collision) { if (collision.rigidbody) {} else { osuuSeinaan = true; }
and if osuuSeinaan is false, I try to rigidoby.moveposition, but can't get the Input.GetAxis("$$anonymous$$ouse X") to that command. When there's no variables but numbers in rigidbody.moveposition, it still goes through the wall.
Your answer
