Move the Main Camera on Collision 2D

void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.transform.name==“Ball”){

			if(Camera.main.transform.position.y>(transform.position.y)){
				StartCoroutine("PositionCameraDown");	
			}else{
				StartCoroutine("PositionCameraUp");	
			}
							

		}
	}
	
	public IEnumerator PositionCameraUp()
    {
        while(Camera.main.transform.position.y<transform.position.y){
				Camera.main.transform.position = new Vector3(Camera.main.transform.position.x,Camera.main.transform.position.y+0.02f,Camera.main.transform.position.z);
				yield return new WaitForSeconds(0.0000000000000000000000000000001f);
		}
    }
	
	public IEnumerator PositionCameraDown()
    {
        while(Camera.main.transform.position.y>transform.position.y){
				Camera.main.transform.position = new Vector3(Camera.main.transform.position.x,Camera.main.transform.position.y-0.02f,Camera.main.transform.position.z);
				yield return new WaitForSeconds(0.0000000000000000000000000000001f);
		}
    }

I tried this code sample to move the camera to the GameObject’s y position when the Ball
object gets collide with it. But the camera moves very slowly. As I increased the number of zeros in 0.0000000000000000000000000000001f the speed doesn’t increase.

What is the reason for that? I want to move the camera quickly. Is there another way to do this?

Hi, there are some things that I don’t understand in your code.


First of all why did you create two identical functions (PositionCameraUp/Down)? I think you did it because you must check if the camera y value is “grater” or “less” than the current y position. Instead of checking “grater” or “less” you could check if it isn’t “equal”, so you could use only one function.


The second thing is this line:

 Camera.main.transform.position = new Vector3(Camera.main.transform.position.x,Camera.main.transform.position.y,Camera.main.transform.position.z);

This line is the same thing than say:

Camera.main.transform.position = Camera.main.transform.position;

That is the same thing than say 1 = 1 You wanted probably use transfom.position.y for the target position.


The last thing is the yield return new WaitForSeconds(0.0000000000000000000000000000001f);
I think that use a number with 31 decimals isn’t very useful because Unity will probably round it to 0. You could use yield return null; instead.


If you would like to move an object to a target position you could use Vector3.MoveTowards that does exactly what you want.

Here I modified the script, try it and say if it is what you wanted.


    public float cameraSpeed = 0.5f;

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.transform.name == "Ball")
        {
            StartCoroutine("PositionCamera");
        }
    }

    public IEnumerator PositionCamera()
    {
        while (Camera.main.transform.position.y != transform.position.y)
        {
            Camera.main.transform.position = Vector3.MoveTowards(Camera.main.transform.position, new Vector3(Camera.main.transform.position.x, transform.position.y, Camera.main.transform.position.z), cameraSpeed);
            yield return null;
        }
    }