Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Move Camera with Player - Not Follow Player

Discussion in 'Scripting' started by RVZ, Aug 28, 2015.

  1. RVZ

    RVZ

    Joined:
    Jul 8, 2013
    Posts:
    4
    I've gone through a lot of the camera movement questions, but not quite finding what I'm looking for. The game is a top-down game, so I want the camera to follow the player when he reaches close to the border of the view-able screen, but not follow him the whole time. And I don't want the camera moving when the edge of a level is reached.

    So for example, if I have the player's position, I can do something like this:

    Code (CSharp):
    1. if (player.localPosition.x < 10.0f && player.localPosition.x > -10.0f) {
    2.         transform.position = player.position - cameraPos;
    3.         }
    This lets the camera follow the player, but once he reaches close to the end of the left and right, the camera will stop following, and the player can move around freely. I can obviously do something similar to up and down. The problem is once he's out of the side zones where he's not being followed, the camera kinda snaps back into position following him again, which is not what I want. So I rather want to camera just to move up and down, and side to side when he reaches the ends of the view-able screen, and not follow him.

    Hope this makes sense? ;-) Any suggestions?
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    I'm guessing you mean following to the perimeter? Something like this would work:

    Code (CSharp):
    1. Vector3 offset = player.transform.localPosition;
    2.         offset.x = Mathf.Clamp(offset.x , - 10.0f, 10.0f);
    3.         offset.y = Mathf.Clamp(offset.y , - 10.0f, 10.0f);
    4.  
    5.         transform.position = offset - cameraPos;
    With this you need no if statement, as it'll constantly be putting you closest to the player while staying in the 'bounds' you asked for (in this case 10 and -10).
     
  3. RVZ

    RVZ

    Joined:
    Jul 8, 2013
    Posts:
    4
    Thanks for the reply. Not sure why this is happening, but if I use this, as soon as I start it jumps to the bottom left corner of my level, no matter where I place the player character. I also can't see the player character, but the camera is moving when I move the character.
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Can you show me the whole thing please? It sounds like another part of the script is affecting it, as I tested my code in a blank scene with one camera and a cube called 'player', and it worked fine. I think I know what it is, it's trying to clamp a null value somewhere.
     
  5. RVZ

    RVZ

    Joined:
    Jul 8, 2013
    Posts:
    4
    Sure. I've been reading up on the Math.Clamp part and it looks like this is what's needed, just can't get it working correctly. This was is the rest of the script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraMovement : MonoBehaviour { private Transform player; private Vector3 cameraPos;
    5.  
    6.     void Awake()
    7.     {
    8.         player = GameObject.FindGameObjectWithTag("Player").transform;
    9.         cameraPos = player.position-transform.position;
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         if (player.localPosition.x < 10.0f && player.localPosition.x > -10.0f) {
    15.         transform.position = player.position - cameraPos;
    16.     }
    17. }
    18. }
     
  6. RVZ

    RVZ

    Joined:
    Jul 8, 2013
    Posts:
    4
    Ok, got one part of my issue sorted, thanks for pointing me in the right direction with the Mathf.Clamp command. This works fine for me for the borders of the game on the camera.

    Code (CSharp):
    1. transform.localPosition = new Vector2(
    2.         Mathf.Clamp(transform.localPosition .x, xMin, xMax),
    3.         Mathf.Clamp(transform.localPosition .y, yMin, yMax)
    4.     );
    It already works a lot better, but I still don't want the camera following the player once he leaves the border parts. How can I move the camera at the same speed as the player, once the player reaches a certain distance from the border of the viewable screen, and the level continues in that direction?
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
  8. dvazhenind

    dvazhenind

    Joined:
    Nov 27, 2017
    Posts:
    1
    I hope this will help to people. Thanks to Brackeys

    Code (CSharp):
    1. void Start () {
    2.        
    3.         offSet = transform.position - player.transform.position;
    4.        
    5.     }
    6.    
    7.     void LateUpdate () {
    8.         Vector3 desiredPosition = player.transform.position + offSet;
    9.         Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
    10.         transform.position = smoothedPosition;
    11.     }