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.