- Home /
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
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 ?
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;
}
}
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 ;
}
}