- Home /
Avoid one gameObject skin in another gameObject
Let say i have two gameObject, one is sphere another is cube. While sphere position is control by mouse cursor(sphere follow the move position). But my sphere will skin in the cube, when my cursor near to cube 1B.
I perfer the sphere will not skin in the cube.
Is Unity3D have any way to make the sphere look like in picutre 1A. The sphere just touch or not skin in the cube like.
I have try add collider and rigidbody, but it is still skin in.
Update 1
i have a hand model that have animation, i expect the hand can grasp something in the game.(i trigger the hand animation to grasp the object)
But what i got is the hand will skin in the object like 2B, i hope the hand will automatically stop when it touch the object like 2A.
Answer by save · Jul 20, 2011 at 06:47 AM
It depends on how you control the sphere. If you use transform.position or transform.translate for positioning you disallow all rigidbody calculations for physic movement. How about attaching a spring joint from an empty gameobject which is controlled by the mouse to the sphere?
This mean there is no easy way the sphere will automatically stop when touched the cube? It is require coding, can not just simply apply some physics components to make sphere stop when touch the cube.
It wont require much coding if you let the physics take care of the movement, doing this will also be optimized in both visual aspect (as the physics engine is hard to outperform codewise) and for CPU.
"How about attaching a spring joint from an empty gameobject which is controlled by the mouse to the sphere". I mostly catch what are you propose to me, it is a great idea. But i'm not so sure how i can implement it. Did you $$anonymous$$d to provide some hint or tutorial source to me? I am still new to Unity 3D.
Answer by Macdude2 · Jul 20, 2011 at 06:50 AM
What you probably want to do is set up a raycast so the sphere raycasts towards the cube. Then if the distance of the raycast is less than half the width of you sphere, you can translate the position of the sphere away from the cube by .5 - raycast.distance.
Code to put on Sphere:
var direction: Vector3;
var hit: RaycastHit;
var newdist = 0.0;
function Update(){
direction = transform.TransformDirection(Vector3.right);
if(Physics.raycast(transform.position, direction, hit)){
if(hit.distance < .5){
newdist = .5 - hit.distance;
transform.translate(-Vector3.right * newdist);
}
}
}
well, above is just my testing for my other big question. $$anonymous$$ay be i should spell that out also.
Your answer
Follow this Question
Related Questions
Collisions don't work properly 1 Answer
collision child with rigidbody in parent 0 Answers
OnTrigger's Moving Behaviour with Other Triggers 0 Answers
Using character controller insted of normal collider 0 Answers
compount collider or other solution 0 Answers