- Home /
2D Platform Shooter Camera Controls C#
So I haven't had much luck finding a solution for this.
I have two parts to my player control - the player object, and a cross-hair. I actually have a lot more but these are the two parents. I originally wasn't going to have an aim feature for the player, so I created a focus area for the camera around my player object. When the player reaches the edge of this box it pushes the camera in that direction. It has some more robust features that can be removed if needed.
I want to adjust this script (or write a new one) that allows the cross-hair to push the focus box as well, but only so long as the player is contained in it. The cross-hair will NOT be able to move the player, but the player WILL be able to move the cross-hair to keep it on screen.
Here's what I have so far
The CameraFollow Script (sorry you have to scroll side-ways to see it all):
public class CameraFollow : MonoBehaviour {
public Controller2D target;
public float verticalOffset;
public float lookAheadDstX;
public float lookSmoothTimeY;
public float lookAheadDstY;
public float lookSmoothTimeX;
public float verticalSmoothTime;
public Vector2 focusAreaSize;
FocusArea focusArea;
float currentLookAheadX;
float targetLookAheadX;
float lookAheadDirX;
float smoothLookVelocityX;
float smoothVelocityY;
bool lookAheadStopped;
void Start() {
focusArea = new FocusArea (target.collider.bounds, focusAreaSize);
}
void LateUpdate() {
focusArea.Update (target.collider.bounds);
Vector2 focusPosition = focusArea.centre + Vector2.up * verticalOffset;
if (focusArea.velocity.x != 0) {
lookAheadDirX = Mathf.Sign (focusArea.velocity.x);
if (Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0) {
lookAheadStopped = false;
targetLookAheadX = lookAheadDirX * lookAheadDstX;
}
else {
if (!lookAheadStopped) {
lookAheadStopped = true;
targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX)/4f;
}
}
}
currentLookAheadX = Mathf.SmoothDamp (currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);
focusPosition.y = Mathf.SmoothDamp (transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
focusPosition += Vector2.right * currentLookAheadX;
transform.position = (Vector3)focusPosition + Vector3.forward * -10;
}
void OnDrawGizmos() {
Gizmos.color = new Color (1, 0, 0, .5f);
Gizmos.DrawCube (focusArea.centre, focusAreaSize);
}
struct FocusArea {
public Vector2 centre;
public Vector2 velocity;
float left,right;
float top,bottom;
public FocusArea(Bounds targetBounds, Vector2 size) {
left = targetBounds.center.x - size.x/2;
right = targetBounds.center.x + size.x/2;
bottom = targetBounds.min.y;
top = targetBounds.min.y + size.y;
velocity = Vector2.zero;
centre = new Vector2((left+right)/2,(top +bottom)/2);
}
public void Update(Bounds targetBounds) {
float shiftX = 0;
if (targetBounds.min.x < left) {
shiftX = targetBounds.min.x - left;
} else if (targetBounds.max.x > right) {
shiftX = targetBounds.max.x - right;
}
left += shiftX;
right += shiftX;
float shiftY = 0;
if (targetBounds.min.y < bottom) {
shiftY = targetBounds.min.y - bottom;
} else if (targetBounds.max.y > top) {
shiftY = targetBounds.max.y - top;
}
top += shiftY;
bottom += shiftY;
centre = new Vector2((left+right)/2,(top +bottom)/2);
velocity = new Vector2 (shiftX, shiftY);
}
}
}
And here is the script for the Cross-Hair, which is actually a 2D sprite that follows the mouse position in world-space:
public class Crosshair : MonoBehaviour {
private Vector3 MouseCoords;
public float MouseSensitivity = 0.1f;
void Update () {
GameObject crosshair = GameObject.Find ("crosshair");
MouseCoords = Input.mousePosition;
MouseCoords = Camera.main.ScreenToWorldPoint (MouseCoords);
crosshair.transform.position = Vector2.Lerp (transform.position, MouseCoords, MouseSensitivity);
print (MouseCoords);
}
}
I'm not quite sure what your "focus box" is.
If the size of your focus box is constant though, couldn't you just clamp the crosshair position within some distance of the player?
// Pseudocode
crosshairX = $$anonymous$$athf.Clamp(playerX - sizeX, playerX + sizeX, crosshairX);
crosshairY = //...
Your answer
Follow this Question
Related Questions
Deadfrontier type crosshair 1 Answer
2D Orthographic Main Camera and UI 0 Answers
Change the background color attribute of a camera in C#? 2 Answers
Camera setup question 2 Answers