camera controller has a bounding box now

master
Jordan Orelli 5 years ago
parent 8536723fe1
commit a7302dc95e

File diff suppressed because it is too large Load Diff

@ -4,16 +4,85 @@ using UnityEngine;
public class CameraController : MonoBehaviour { public class CameraController : MonoBehaviour {
public Transform player; public Transform player;
public Camera camera;
public float maxAcceleration = 1f;
public float maxVelocity = 10f;
private Frame frame;
private Vector3 velocity;
// Start is called before the first frame update // Start is called before the first frame update
void Start() { void Start() {
camera = gameObject.GetComponent<Camera>();
velocity.x = 0;
velocity.y = 0;
velocity.z = 0;
} }
// Update is called once per frame
void Update() { void Update() {
}
// LateUpdate is called once per frame, after Update
void LateUpdate() {
setupFrame();
if (player) { if (player) {
transform.position = new Vector3(player.position.x, player.position.y, player.position.z-10); BoxCollider2D collider = player.GetComponent<BoxCollider2D>();
transform.LookAt(player.transform.position); if (collider.bounds.max.x > frame.topRight.x) {
transform.Translate(new Vector3(collider.bounds.max.x - frame.topRight.x, 0, 0));
}
if (collider.bounds.min.x < frame.topLeft.x) {
transform.Translate(new Vector3(collider.bounds.min.x - frame.topLeft.x, 0, 0));
}
if (collider.bounds.max.y > frame.topLeft.y) {
transform.Translate(new Vector3(0, collider.bounds.max.y - frame.topLeft.y, 0));
} }
if (collider.bounds.min.y < frame.bottomLeft.y) {
transform.Translate(new Vector3(0, collider.bounds.min.y - frame.bottomLeft.y, 0));
}
}
}
void OnDrawGizmosSelected() {
Gizmos.color = Color.white;
Gizmos.DrawLine(frame.topLeft, frame.topRight);
Gizmos.DrawLine(frame.topRight, frame.bottomRight);
Gizmos.DrawLine(frame.bottomRight, frame.bottomLeft);
Gizmos.DrawLine(frame.bottomLeft, frame.topLeft);
if (player) {
BoxCollider2D collider = player.GetComponent<BoxCollider2D>();
Gizmos.color = Color.red;
if (collider.bounds.max.x > frame.topRight.x) {
Gizmos.DrawLine(frame.topRight, frame.bottomRight);
}
if (collider.bounds.min.x < frame.topLeft.x) {
Gizmos.DrawLine(frame.bottomLeft, frame.topLeft);
}
if (collider.bounds.max.y > frame.topLeft.y) {
Gizmos.DrawLine(frame.topLeft, frame.topRight);
}
if (collider.bounds.min.y < frame.bottomLeft.y) {
Gizmos.DrawLine(frame.bottomRight, frame.bottomLeft);
}
}
}
void setupFrame() {
int height = Screen.height;
int width = Screen.width;
frame.topLeft = camera.ScreenToWorldPoint(new Vector3(width / 2 - width / 6, height / 2 + height / 3, 1));
frame.topRight = camera.ScreenToWorldPoint(new Vector3(width / 2 + width / 6, height / 2 + height / 3, 1));
frame.bottomLeft = camera.ScreenToWorldPoint(new Vector3(width / 2 - width / 6, height / 2 - height / 3, 1));
frame.bottomRight = camera.ScreenToWorldPoint(new Vector3(width / 2 + width / 6, height / 2 - height / 3, 1));
}
struct Frame {
public Vector3 topLeft;
public Vector3 topRight;
public Vector3 bottomLeft;
public Vector3 bottomRight;
} }
} }

@ -38,7 +38,6 @@ public class MoveController : MonoBehaviour
if (velocity.y != 0) { if (velocity.y != 0) {
VerticalCollisions(ref velocity); VerticalCollisions(ref velocity);
} }
Debug.LogFormat("Moving x: {0} y: {1}", velocity.x, velocity.y);
transform.Translate(velocity); transform.Translate(velocity);
} }
@ -53,11 +52,9 @@ public class MoveController : MonoBehaviour
Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red); Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
if (hit) { if (hit) {
Debug.LogFormat("Vertical Hit with distance: {0}", hit.distance);
if (directionY > 0) { if (directionY > 0) {
collisions.above = true; collisions.above = true;
} else { } else {
Debug.LogFormat("Hit is below: we are now grounded");
collisions.below = true; collisions.below = true;
} }
velocity.y = (hit.distance - skinWidth) * directionY; velocity.y = (hit.distance - skinWidth) * directionY;

@ -3,6 +3,7 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
[RequireComponent(typeof(MoveController))] [RequireComponent(typeof(MoveController))]
[RequireComponent(typeof(BoxCollider2D))]
public class PlayerController : MonoBehaviour { public class PlayerController : MonoBehaviour {
public float jumpHeight = 4f; public float jumpHeight = 4f;
public float timeToJumpApex = 0.4f; public float timeToJumpApex = 0.4f;
@ -26,28 +27,19 @@ public class PlayerController : MonoBehaviour {
void Update() { void Update() {
frameCount++; frameCount++;
Debug.LogFormat("Player Update Start {0} ------------------------", frameCount);
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Debug.LogFormat("Position x: {0} y: {1}", transform.position.x, transform.position.y);
Debug.LogFormat("Input x: {0} y: {1}", input.x, input.y);
float targetX = input.x * moveSpeed; float targetX = input.x * moveSpeed;
Debug.LogFormat("Target dx: {0}", targetX);
Debug.LogFormat("Above: {0}", moveController.collisions.above);
if (moveController.collisions.above) { if (moveController.collisions.above) {
velocity.y = 0; velocity.y = 0;
} }
Debug.LogFormat("Left: {0}", moveController.collisions.left);
Debug.LogFormat("Right: {0}", moveController.collisions.right);
if (moveController.collisions.left || moveController.collisions.right) { if (moveController.collisions.left || moveController.collisions.right) {
velocity.x = 0; velocity.x = 0;
} }
Debug.LogFormat("Grounded: {0}", moveController.isGrounded);
if (moveController.isGrounded) { if (moveController.isGrounded) {
if (Input.GetButtonDown("Jump")) { if (Input.GetButtonDown("Jump")) {
Debug.Log("JUMP");
velocity.y = jumpVelocity; velocity.y = jumpVelocity;
} else { } else {
velocity.y = gravity * Time.deltaTime; velocity.y = gravity * Time.deltaTime;
@ -61,9 +53,7 @@ public class PlayerController : MonoBehaviour {
} }
} }
Debug.LogFormat("Velocity x: {0} y: {1}", velocity.x, velocity.y);
moveController.Move(velocity * Time.deltaTime); moveController.Move(velocity * Time.deltaTime);
Debug.LogFormat("Grounded: {0}", moveController.isGrounded);
} }
void OnDestroy() { void OnDestroy() {

@ -9,6 +9,7 @@
"com.unity.ide.vscode": "1.1.4", "com.unity.ide.vscode": "1.1.4",
"com.unity.multiplayer-hlapi": "1.0.4", "com.unity.multiplayer-hlapi": "1.0.4",
"com.unity.purchasing": "2.0.6", "com.unity.purchasing": "2.0.6",
"com.unity.quicksearch": "1.6.0-preview.6",
"com.unity.test-framework": "1.1.11", "com.unity.test-framework": "1.1.11",
"com.unity.textmeshpro": "2.0.1", "com.unity.textmeshpro": "2.0.1",
"com.unity.timeline": "1.2.6", "com.unity.timeline": "1.2.6",

Loading…
Cancel
Save