Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Krigo · Aug 05, 2015 at 02:45 PM · 2d2d camera

How to make border for camera?

in 2d game camera follow to the player, then he go to the right. How to make, that camera will be stoped/blocked if player go to the left?

Sorry about english. c#.

Comment
Add comment · Show 4
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 Krigo · Aug 05, 2015 at 01:53 PM 0
Share

just simple game, like mario.

avatar image Hellium · Aug 05, 2015 at 01:59 PM 0
Share

It doesn't bring information ...

How do you currently manage you camera ? Through a script ? Attached to your player ?

How do you move your player, through a script too I guess ?

avatar image Krigo · Aug 05, 2015 at 02:10 PM 0
Share

Camera:

  using UnityEngine;
     using System.Collections;
     
     public class CameraTracksPlayer : $$anonymous$$onoBehaviour {
         
         Transform player;
         
         float offsetX;
     
         void Start () {
             GameObject player_go = GameObject.FindGameObjectWithTag("Player");
             
             if(player_go == null) {
                 Debug.LogError("Couldn't find an object with tag 'Player'!");
                 return;
             }
             
             player = player_go.transform;
             
             offsetX = transform.position.x - player.position.x;
         }
     
         void Update () {
             if(player != null) {
                 Vector3 pos = transform.position;
                 pos.x = player.position.x + offsetX;
                 transform.position = pos;
             }
     
         }
     }


Player: using UnityEngine; using System.Collections;

 public class characterController : $$anonymous$$onoBehaviour {
     public float maxSpeed = 10f;
     public float jumpForce = 300f;
     bool facingRight = true;
     bool grounded;
     public Transform groundCheck;
     public float groundRadius = 0.2f;
     public Layer$$anonymous$$ask whatIsGround;
     public float score;
     public float move;
     Animator anim;
     public bool jump = false;    
 
 
 
     void Start () {
         anim = transform.GetComponent<Animator>();
     }
     
 
     void FixedUpdate () {
         if(jump)
         {
             anim.SetTrigger("Jump");
             GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce));
             jump = false;
         }
         
     grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 
         move = Input.GetAxis ("Horizontal");
         anim.SetFloat ("Speed", $$anonymous$$athf.Abs (move));
 
     }
     
     void Update(){
 
         if (grounded && (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.W)||Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.UpArrow))) {
             GetComponent<Rigidbody2D>().AddForce (new Vector2(0f,jumpForce));
             jump = true;
 
         }
         GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
         //
         
         if (move > 0 && !facingRight)
             Flip ();
         else if (move < 0 && facingRight)
             Flip ();
         
         
         
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Escape))
         {
             Application.Quit();
         }
         
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.R))
         {
             Application.LoadLevel(Application.loadedLevel);
         }
         
         
     }
     
     void Flip(){
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     
 }
 
avatar image Krigo · Aug 05, 2015 at 02:16 PM 0
Share

THAN$$anonymous$$ YOU!))

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Aug 05, 2015 at 02:14 PM

The answer is quite obvious then :

CameraTracksPlayer :

 void Update () {
     if(player != null) {
         Vector3 pos = transform.position;
         
         // Make sure the camera goes only to the right
         if( player.position.x + offsetX > pos.x)
             pos.x = player.position.x + offsetX;
             
         transform.position = pos;
     }    
 }

Thought, this script won't prevent the character to go to the left.

To prevent the character to go outside the camera :

 // CameraTracksPlayer

 Camera cameraComponent ;
 Vector3 lastValidCharacterPosition ;
 
 void Start () {
     GameObject player_go = GameObject.FindGameObjectWithTag("Player");
 
     if(player_go == null) {
         Debug.LogError("Couldn't find an object with tag 'Player'!");
         return;
     }
 
     player = player_go.transform;
 
     offsetX = transform.position.x - player.position.x;
     
     cameraComponent = GetComponent<Camera>() ;
     lastValidCharacterPosition = transform.position ;
 }
 
 void Update () {
     if(player != null) {
         Vector3 pos = transform.position;
         
         if( camera.WorldToViewportPoint( player.position ).x < 0 )
             player.position = lastValidCharacterPosition ;
         
         // Make sure the camera goes only to the right
         if( player.position.x + offsetX > pos.x)
             pos.x = player.position.x + offsetX;
             
         transform.position = pos;
         
         lastValidCharacterPosition = player.position ;
     }
 
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Fit an orthographic to touch edges of object. 1 Answer

Restrict 2d drag camera movement 1 Answer

Camera movement in a pixel perfect 2D game, how to do it the right way? 0 Answers

Camera script 1 Answer


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