Search Unity

Camera that does Not but Player in the center of the Screen?

Discussion in '2D' started by RuneShiStorm, Jun 22, 2017.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Hi!
    I've been looking everywhere for this but cant find anything...
    Basically, My Player is located in the center of the screen. but I want him to be further down on the Y axis.
    Something like, .y axis - 50... So I get more space above him... Make sense?
    My current script... I dont know what to write or where to put it :S

    Thanks in advance!

    using UnityEngine;
    using System.Collections;



    public class CameraFollow : MonoBehaviour {
    [SerializeField]
    private float xMax;

    [SerializeField]
    private float yMax;

    [SerializeField]
    private float xMin;

    [SerializeField]
    private float yMin;

    private Transform target;

    void Start()
    {
    target = GameObject.Find("Player").transform;
    }

    // Update is called once per frame
    void LateUpdate()
    {
    transform.position = new Vector3(Mathf.Clamp(target.position.x, xMin, xMax), Mathf.Clamp(target.position.y, yMin, yMax), transform.position.z);
    }
    }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Take a look at this page for how to post code on the forums so it's nicer to read: https://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Now, to your question... Suppose you set an offset (height, in this case). You then add that to the target position, and make sure the yMin and yMax accomodate it, too.
    You can do this like:
    Code (csharp):
    1.  
    2. Vector3 offset = new Vector3(0,target.position.y - 2,0);
    3. // Now, use 'offset.y' instead of target.position.y in your position assignment
    4.  
     
  3. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Thanks.
    I will look into that Code-posting thing!

    Im a Unity beginner and cant get around unless I watch Tutorials... Where am I supposed to put this script?
    The ""transform.position = new Vector3(Mathf................."" is to make sure the camera don't go outside the level.
    So I guess Im not gonna put your script in there?
    I want to make sure that my character is located in the bottom of the screen... So I guess "player" is the "target"? And I have to add your script around there? But how?
    Thansk in advance if you have an answer for my noob question :p
     
  4. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    I did something like this... but not working...
    Code (csharp):
    1.  
    2. void Start()
    3.         {
    4.             target = GameObject.Find("Player").transform;
    5.         Vector3 offset = new Vector3(0, target.position.y - 10, 0);
    6.     }
    7.  
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, say from your first post, add that 'offset' bit like so:
    Code (csharp):
    1.  
    2. transform.position = new Vector3(Mathf.Clamp(target.position.x, xMin, xMax), Mathf.Clamp(offset, yMin, yMax), transform.position.z);
    3.  
     
    RuneShiStorm likes this.
  6. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264

    I added this to be able to change the offset from the insperctor;

    Code (csharp):
    1.  
    2.   [SerializeField]
    3.     private float offset;
    4.  
    5.  
    and its working! its giving me a warning message.. but bascially its working.. :)
    Thanska lot!
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's good. Glad it's working. What warning, out of curiousity? :)
     
  8. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    I went back and checked again and the warning is gone. I think my "max y,x Min y,x" number was reset so the camera was wining about that. Sooo,.,.. basically its all god now!
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ahh, cool. Good stuff :)
     
    RuneShiStorm likes this.