- Home /
Is it possible to keep the player offset in place on the screen independent of resolution?
Okay, so I have a camera I've been building for a 2D sidescrolling game. The player needs to stay slightly off to the left - like an original Gradius game:
The camera moves freely from the player, and needs to stay that way. At the moment, I just can't get the offset working right - I keep trying the various screen conversions but I'm not sure what will work - here is my code:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public Transform TrackTarget;
public float dampTime = 0.15f;
public float OffsetFloat = 0.05f;
public bool Ready = false;
private Vector3 velocity = Vector3.zero;
private Vector3 TargetOffset = Vector3.zero;
void Start()
{
TargetOffset = (camera.WorldToViewportPoint(TrackTarget.position * OffsetFloat));
Vector3 point = TrackTarget.position + TargetOffset;
Vector3 Destination = new Vector3(point.x, point.y, transform.position.z);
transform.position = Destination;
}
void FixedUpdate () {
TargetOffset = (camera.WorldToViewportPoint(TrackTarget.position * OffsetFloat));
if (Ready) {
if (TrackTarget)
{
Vector3 point = TrackTarget.position + TargetOffset;
Vector3 Destination = new Vector3(point.x, point.y, transform.position.z);
transform.position = Vector3.SmoothDamp(transform.position, Destination, ref velocity, dampTime);
}
}
}
void LateUpdate()
{
if (Ready) {
Vector3 CameraPosition = transform.position;
CameraPosition.z = -10.00f;
transform.position = CameraPosition;
}
}
}
as far as i understand you want to move the camera freely around the scene and let the ship follow it so that the relative distance from the left screen border and ship is always fixed...? if that's so I would use camera.ViewportToWorldPoint, not sure what u need to do tho...
That is about where I am at the moment, unsure on the second half.
This is just theory as I can't test it AT$$anonymous$$ but could you set your ships movement up and down to be 2 floats for x and y. If you press up the increase y by .01 (or whatever works for you) and the same for x. With x you might want to restrict x to between .1 and .5 with .5 being the middle of the scene.
The in update move the ship to the world point with ViewportToWorldPoint as
Vector3 $$anonymous$$yPos = cacamera.ViewportToWorldPoint ( new Vector3 (myX, myY, camera.nearClipPlane));
Typing on a phone so sorry for typos!
Huh? The vehicle the camera is following is moved with physics, and there are no issues with how that works. This is related to tracking and keeping the vehicle offset from the center of the camera, as old-school 2D shooters used to do.
Answer by fafase · Jan 14, 2015 at 10:29 AM
My two cents on this, get the boundaries of the screen on Start, then convert the positions to world position, modify the position by the ratio you want to be inside the screen, then clamp the ship x and y positions.
Here for converting to world position : http://answers.unity3d.com/questions/501893/calculating-2d-camera-bounds.html
then you create some game object that you attach to the camera so that they follow.
Finally, you clamp the ship position using those 4 objects.