Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by sachith4943 · Aug 12, 2018 at 11:33 AM · camera2dcollisioncamera movement

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         }
     }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sachith4943 · Aug 12, 2018 at 03:22 PM 0
Share

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.

avatar image MT369MT · Aug 12, 2018 at 03:47 PM 0
Share

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).

avatar image sachith4943 MT369MT · Aug 12, 2018 at 04:26 PM 0
Share

Can you please give me an example code. Thank you!!

It cannot apply inside void OnCollisionEnter2D(Collision2D col) function

avatar image MT369MT sachith4943 · Aug 12, 2018 at 05:35 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

315 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Bounds in Complex Shape 1 Answer

Unity 2D camera Bounds for player 0 Answers

Camera bounds with zoomable camera 1 Answer

Trying to collide in grid like movement 1 Answer

Character hits invisible ghost collision when jumping against objects while pushing against them 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges