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

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);
}
}

Take a look at this page for how to post code on the forums so it’s nicer to read: Using code tags properly - Unity Engine - Unity Discussions

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:

Vector3 offset = new Vector3(0,target.position.y - 2,0);
// Now, use 'offset.y' instead of target.position.y in your position assignment

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 :stuck_out_tongue:

I did something like this… but not working…

void Start()
        {
            target = GameObject.Find("Player").transform;
        Vector3 offset = new Vector3(0, target.position.y - 10, 0);
    }

Okay, say from your first post, add that ‘offset’ bit like so:

transform.position = new Vector3(Mathf.Clamp(target.position.x, xMin, xMax), Mathf.Clamp(offset, yMin, yMax), transform.position.z);
1 Like

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

  [SerializeField]
    private float offset;

and its working! its giving me a warning message… but bascially its working… :slight_smile:
Thanska lot!

That’s good. Glad it’s working. What warning, out of curiousity? :slight_smile:

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!

Ahh, cool. Good stuff :slight_smile:

1 Like