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?
Answer by MT369MT · Aug 12, 2018 at 03:07 PM
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;
}
}
Sorry!! I made a mistake in the code. I want to move the camera to the position of the collided gameObject from a different position from within 1second, not just suddenly move to the position.
I've done it by adding or subtracting 0.02f to y position of camera gradually by adding a delay.
But it moves the camera slowly. I want to make it faster.
Vector3.$$anonymous$$oveTowards doesn't move it instantly. You could change the speed of it (I used 0.5f but if you need it slowlier change it like you want).
Can you please give me an example code. Thank you!!
It cannot apply inside void OnCollisionEnter2D(Collision2D col) function
In my first answer there is already the code with Vector3.$$anonymous$$oveTowards:
Camera.main.transform.position = Vector3.$$anonymous$$oveTowards(Camera.main.transform.position, new Vector3(Camera.main.transform.position.x, transform.position.y, Camera.main.transform.position.z), cameraSpeed);
Anyway to use Vector3.$$anonymous$$oveTowards you must write something like this:
ObjectTo$$anonymous$$ove.transform.position = Vector3.$$anonymous$$oveTowards (ObjectTo$$anonymous$$ove.transform.position, Target.transform.position, Speed * Time.deltaTime);
In the documentation you see more informations:
https://docs.unity3d.com/ScriptReference/Vector3.$$anonymous$$oveTowards.html