Collision Detection issue

Hi,

I am still new with Unity and not really 100% familiar with even some of the syntax I am using. I have created a 2D sprite image and place it in Unity and used it as a 2D sprite animation. I have created to object the other one with a 2D rigid body and a 2D box collision and the other object with a 2D box collision only. I would just like to ask help on why when I use this code on my object the collision can be detected or is working fine.

Vector3 x;

//and place this one on my update function
x = Input.GetAxis("Horizontal") * transform.right * speed * Time.deltaTime;
	transform.Translate(x);
Vector3 position;

// and place this on my update function
	if (Input.GetKey(KeyCode.A))
		{
			position.x -= speed * Time.deltaTime;
			position.z = -0.15f;
			transform.position = position;
		}
		else if (Input.GetKey(KeyCode.D))
		{
			position.x += speed * Time.deltaTime;
			position.z = -0.15f;
			transform.position = position;
		}
		else 
		{
			position.z = 0.5f;
			transform.position = position;
		}

I would have used the first part of the code where the collision is being detected. However, my issue there is I do not know where to put the z index or Vector3 (I am not sure I am using the correct terms) so that each time “A” or “D” is not being pressed the z value on the 3D plane will update and if “A” or “D” again is being pressed the z value on the 3D plane will update again (just like my 2nd example). If there is any clarification or additional information that is needed on my end just tell I am not sure if I got everything and said everything properly and correctly. Suggestion and additional learning I would really appreciate it. :slight_smile:

I ran into that problem when i first started working with collision too. The problem is that you are manually setting the object’s position rather than applying physics force to it. If you want physics to act on it, you need to use ApplyForce() (i think?)

basically, the collisionis working and the engine is saying “collision occured. stop movement” but your script continually is saying “move to this precise location” so your object will keep being placed at that exact location regardless.

if you want to manually set its position like you’re doing, you’d need a OnCollisionEnter() check and then manually move or freeze your object in reaction to the detected collision.

if you want tighter more exact control over your object, set positions manually. If you’re ok with a bit of unpredictability, use physics.

Hey SirWeeble,

Thanks for the reply really appreciate it really got some insights when you explained my code more I got another insight on it :). Abut your last paragraph you pointed out that i could use physics how do I do that? I am just really new with Unity and not really familiar with all.

“how do i use physics?” is quite a open ended question :stuck_out_tongue: but I’d suggest starting with these:

http://docs.unity3d.com/Documentation/Manual/Physics.html
http://docs.unity3d.com/Documentation/Manual/Physics.html