How to create a collision for two game objects

Hi,

I am just starting out with unity and I did a little research and a little programming already using C#. Can anyone help me or give me an advice or point me to the right direction on how make two objects collide “smoothly” or “solidly”. Because I have created two spheres and one sphere can move along the x,y, and axis with keyboard keypads like the WASD and the arrows keypads using transform.Translate(); and the other sphere is just there in the 3D space and not moving. Though what I want to happen is when I move the player closer to the other sphere that is just sitting there and doing nothing and when they collide with each other I want both sphere’s to act like solid object. My friend tried this function that I am only familiar with

void OnTriggerEnter(Collider otherObject)
{
if (otherObject.tag == “PlayerBase”)
{

float AmtToMoveX = -Input.acceleration.x * PlayerSpeed * Time.deltaTime;
float AmtToMoveY = -Input.acceleration.y * PlayerSpeed * Time.deltaTime;
///*
if (transform.position.x > 0)
{
AmtToMoveX += 2.5f;
}
else if (transform.position.x < 0)
{
AmtToMoveX -= 2.5f;
}

if (transform.position.y > 0)
{
AmtToMoveY += 2.5f;
}
else if (transform.position.y < 0)
{
AmtToMoveY -= 2.5f;
}
transform.position = new Vector3(AmtToMoveX, AmtToMoveY, transform.position.z);
}
}

The problem with this code is when the sphere that can move and gets closer to the other sphere and its hits the sphere the sphere that moves bounces back off the other sphere. What I want to happen is even though I keep on pushing the sphere that can move onto the other sphere is wont bounce back and it will just remain there close to or sticking the other sphere that should be acting like a solid object.

I have already attached a Sphere Collider on both sphere’s and I have a feeling this component would can help a lot but I do not know how to use it. Or if there is another way to do this other Collider functions that I could use. Any help would be really appreciated. I would really appreciate it more if it is just a guide or advice on how to do it and I will do the coding and logic for it so I can also practice my C# coding along the way. :slight_smile:

What I think you should do is add a different physics material, here is more on it in the documentation. Unity - Manual: Physic Material asset reference

Oh and add code tags so it is easier to read :slight_smile: http://forum.unity3d.com/threads/143875-Using-code-tags-properly

edit: just seen that Sphere Collider has static collider so you be better off using that :slight_smile:

Hi Rome,

Thanks for the tip about the code tags, copy and pasting the codes without the code tags was really a headache for me too, the code tags helps make things easier thanks! :slight_smile:

I kiinda played around with the physics material already and it helped a lot. Oh by the way I found out what I did wrong that even though I placed a sphere collier on the sphere game object that I have created and the reason why it did not work is because the itTrigger component of the sphere collider was checked. Thanks a many again. :slight_smile: