Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Xleg · Oct 12, 2020 at 05:26 PM · cameracamera-movementswitch camerascamera-scrolling

2D camera switch

Hello I am trying to make a seamless endless game.
I found this tutorial which code I used. There are 3 cameras. One main, one left and one right. The main camera has a movement script, which handles the camera movement and the camera switch. The player is attached to the main camera. The problem is that without the player, everything works fine. But when the player is attached to the camera and you move from the right to the left, the player disappears when leaving the end of the world. Normally the right camera should be activated and when the main camera leave the whole world it teleports the player to the other end. In the scene you can see that it's working but in the game view the player disappears. The other way around (from left to right) it is working correct. But there is no big difference in the code.

Here is my CamMove script, which is attached to the main camera:

 public class CamMove : MonoBehaviour
 {
     private float _z;
     private Vector3 _position;
     
     public Joystick joystick;
     
     [Tooltip("The width of the world")]
     public float worldWidth = 57.6f;
 
     [Tooltip("The speed of the camera")]
     public float speed = 10;
 
     [Tooltip("The position of the left-most screen from the perspective of the 
      camera")]
     public float left = -19.2f;
 
     [Tooltip("The position of the right-most screen from the perspective of the 
       camera")]
     public float right = 19.2f;
 
     [Tooltip("The left most position allowed by the camera in the world")]
     private float _leftMax = -38.4f;
 
     [Tooltip("The right most position allowed by the camera in the world")]
     private float _rightMax = 38.4f;
 
     [Tooltip("The left trailing camera")]
     public Transform leftCam;
 
     [Tooltip("The right leading camera")]
     public Transform rightCam;
     
     public Vector3 PositionXY => new Vector3(transform.position.x, 
                                              transform.position.y, 0);
     // Start is called before the first frame update
     void Start()
     {
         _z = transform.position.z;
         _position = transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         var horizontal = joystick.Horizontal;
         _position += Vector3.right * horizontal * speed * Time.deltaTime;
 
         if (_position.x < _leftMax)
         {
             _position = new Vector3(right, 0, _z);
         }
 
         if (_position.x > _rightMax)
         {
             _position = new Vector3(left, 0, _z);
         }
     }
 
     private void LateUpdate()
     {
         transform.position = _position;
         leftCam.position = new Vector3(-1 * worldWidth, 0, _z) + 
                            transform.position;
         rightCam.position = new Vector3(worldWidth, 0, _z) + 
                             transform.position;
     }
 }
 

The clear flags of my cameras are set to 'Don't Clear' and the left and right cam are duplicates of the main camera. Has anyone a idea why it's only working for one side?

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
Best Answer

Answer by Xleg · Oct 19, 2020 at 03:04 PM

There's an extra camera to show the player. In the next video you can see it.

Comment
Add comment · 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

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

217 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

Related Questions

Is there any way to keep game object withing the limit of Camera FOV 3 Answers

How to start with camera and Airplane for the game ? 1 Answer

Issue with clamping camera 1 Answer

Cinemachine Confiner 2D shift my camera view 0 Answers

Refining a 3d side scroller camera script 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