- Home /
C# Gameobject Rigidbody Mouse Collision
I have a gameobject with a rigidbody attached to it. I would like to make it so will collide with other gameobjects when I move it with the mouse. Is there a SimpleMove function for rigidbodies similar to CharacterController.SimpleMove?
Vector3 pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
transform.position = Camera.main.ScreenToWorldPoint(pos);
I already looked up the API and I've tried both RigidBody.Addforce and Rigidbody.$$anonymous$$ovePosition. Neither of them worked the way I was thinking. Addforce makes the rigidbody float around and not collide. $$anonymous$$ovePosition makes the gameobject go from mouseposition to it's original transform.position.
Vector3 pos = Input.mousePosition;
Vector3 pos2 = transform.position;
pos.z = pos2.z - Camera.main.transform.position.z;
pos2 = Camera.main.ScreenToWorldPoint(pos);
rigidbody.AddForce (pos2 * 5);
Vector3 pos = Input.mousePosition;
Vector3 pos2 = transform.position;
pos.z = pos2.z - Camera.main.transform.position.z;
pos2 = Camera.main.ScreenToWorldPoint(pos);
rigidbody.$$anonymous$$ovePosition(pos2 * 5);
Simple version
float distance;
void Start(){
distance = Vector3.Distance(/*camera*/.position, /*movingobject*/.position);
}
void FixedUpdate(){
Vector3 mouse = Input.mousePosition;
Vector3 scr = cam.ScreenToWorldPoint(mouse.x, mouse.y, distance);
rigidbody.$$anonymous$$ovePosition(scr);
}
Answer by Saad_Khawaja · Jul 31, 2014 at 12:59 AM
Use rigidbody.MovePosition - I don't think you're using it correctly.
I wrote this example a while back. http://saadkhawaja.com/move-3d-object-mouse-make-collide/
Your answer
Follow this Question
Related Questions
Get GameObject That Was Last Clicked? 2 Answers
C# Preserving GameObjects' Previous Meshes 1 Answer
C# Plane Detecting a Gameobject 1 Answer
C# Reverting GameObject to Original 1 Answer