- Home /
Prevent colliders intersecting.
I've got a cube with a collider and rigid body attached that follows the mouse position around the screen so that the centre of the cube is where the mouse position is. This means that if I move the mouse onto the side of another object, half of the cube is inside the other object. I've tried playing around with rigid body settings however I can't stop the cube following the mouse to intersect with other objects. Any ideas?
Forgot to mention, when I do use a rigid body with gravity on, when the cube hits another object it just flies off in a different direction, even after playing with all the rigid body settings.
I was originally until I realised that might be the problem, I've got it set as a child object to an empty game object that uses transform.position to set it's position to the mouse position.
Answer by Saad_Khawaja · Jul 17, 2014 at 12:54 PM
If you want it to collide, you can not use Transform.position or Transform.Translate because that means you are teleporting the object to that position.
You need to use CharacterController.Move or .SimpleMove, or use Rigidbody.AddForce, Rigidbody.Moveposition to make sure that an object collides with the other.
Code Snippet: Add Rigidbody to your cube and then attach the following script:
using UnityEngine;
using System.Collections;
public class MoveWithMouse : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 v3 = Input.mousePosition;
v3.z = 10.0f;
v3 = Camera.main.ScreenToWorldPoint(v3);
this.gameObject.rigidbody.MovePosition(v3);
}
}
Ok but how would I do that if I'm trying to follow the mouse position seeing as the only way I know where the mouse is, is by a set of points.
rigidbody.$$anonymous$$ovePosition does exactly what you want. Please check the code above now.
Your answer
Follow this Question
Related Questions
Collider not stoping the object second time 1 Answer
Advice for Intersecting Colliders 0 Answers
Sprites doens't collide properly or bypass each other 0 Answers
How Do I Create Elastic Ropes For a Wrestling Ring? 1 Answer
A node in a childnode? 1 Answer