- Home /
Can I keep a player on the left side of the screen in relation to view resolution?
I'm working on my latest project, which is a action sidescroller, primarily 2D but using a perspective camera for 3D effect. I've gotten the basics working - design, etc, and now fixing the camera so that it follows correctly. However, I'd like to keep the player on the left side of the viewport - not actually modifying the player's position, but the camera following an offset. I have it partially worked out, but it broke the moment I started playing around with other alternative resolutions - say, from web standard, standalone, to mobile devices. This is what I want to do:
Here is the code I have so far, the offset is already in place and does work, but it's not set for multiple resolutions - that is what I am trying to figure out:
public static GameCamera Instance;
public Vector3 TargetOffset = Vector3.zero;
public Transform TrackTarget;
public float dampTime = 0.15f;
public bool Ready = false;
private Vector3 velocity = Vector3.zero;
void Awake()
{
Instance = this;
}
void Start()
{
Vector3 point = TrackTarget.position + TargetOffset;
Vector3 Destination = new Vector3(point.x, transform.position.y, transform.position.x);
transform.position = Destination;
}
void FixedUpdate () {
if (Ready) {
if (TrackTarget)
{
Vector3 point = TrackTarget.position + TargetOffset;
Vector3 Destination = new Vector3(point.x, transform.position.y, transform.position.x);
transform.position = Vector3.SmoothDamp(transform.position, Destination, ref velocity, dampTime);
}
}
}
void LateUpdate()
{
if (Ready) {
Vector3 CameraPosition = transform.position;
CameraPosition.z = -30.00f;
transform.position = CameraPosition;
}
}
Can you just make the camera a child of the player and move the camera so it "looks" offset. Then the camera will follow the player whenever it moves and will remained offset?
No. The camera needs to remain independent in order to be unaffected by the player's rotation.
And as an additional comment, the issue still stands even when the camera is a child of the player. It does not keep the player in focus automatically. If the resolution is adjusted, or changed to a different device, the player may not even be on screen without some moderately heavy code.