Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CameraMovement : MonoBehaviour {
- /*
- public GameObject player;
- public float smoothSpeed = 40f;
- private Vector3 moveAmountFinal;
- private Vector2 mousePos;
- private Vector2 view;
- private Vector2 moveAmount;
- private bool isOutside;
- // Update is called once per frame
- void FixedUpdate () {
- /*Vector3 view = Camera.main.ScreenToViewportPoint(Input.mousePosition); // This works!!! It detects whether or not your mouse is outside of the game
- bool isOutside = view.x < 0 || view.x > 1 || view.y < 0 || view.y > 1;
- if (isOutside) print("OUTSIDE");* /
- mousePos = (Vector2)Input.mousePosition;
- view = (Vector2)Camera.main.ScreenToViewportPoint(mousePos); // This works!!! It detects whether or not your mouse is outside of the game
- isOutside = view.x < 0 || view.x > 1 || view.y < 0 || view.y > 1;
- if (!isOutside)
- {
- moveAmount = Vector2.Lerp((Vector2)player.transform.position, ((Vector2)Camera.main.ScreenToWorldPoint(mousePos) + (Vector2)player.transform.position) / 2, smoothSpeed * Time.deltaTime);
- // Vector2 moveAmount = Vector2.Lerp((Vector2)player.transform.position, (new Vector2((mousePos.x + player.transform.position.x)/2, (mousePos.y + player.transform.position.y) / 2)), smoothSpeed * Time.deltaTime);
- moveAmountFinal = new Vector3(moveAmount.x, moveAmount.y, -10);
- Camera.main.transform.position = moveAmountFinal;
- }
- // Camera.main.transform.position = new Vector3(player.transform.position.x,player.transform.position.y,-10); I don't understand why this works fine, but the rest doesn't!
- // Camera.main.transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, -10); // FOUND THE PROBLEM!! It is the mouseposition, it isn't using the right units or something SOOOOO I need to use ScreenToWorldPoint with mouse positions!
- }
- */
- /*
- public GameObject player;
- public float smoothSpeed = 8f;
- public float moveSpeed = 3f;
- public Vector3 targetPos;
- public Vector3 previousMousePos;
- private void Start()
- {
- targetPos = Camera.main.transform.position;
- previousMousePos = new Vector3(0, 0, 0);
- }
- // Update is called once per frame
- private void Update()
- {
- Vector3 mousePos = Input.mousePosition;
- mousePos = Camera.main.ScreenToWorldPoint(mousePos);
- Vector3 view = Camera.main.ScreenToViewportPoint(mousePos); // This works!!! It detects whether or not your mouse is outside of the game
- bool outside = view.x < 0 || view.x > 1 || view.y < 0 || view.y > 1;
- if (!outside && mousePos != previousMousePos)
- {
- Vector3 distance = ((Camera.main.transform.position + mousePos) / 2) - (Camera.main.transform.position); // This is the distance from the midpoint to the camera
- // print(distance + " | " + moveSpeed + " | " + (distance * moveSpeed)); // Tests if multiplying a vector by a float multiplies all parts (x,y,z,etc.), and it does
- targetPos -= distance * moveSpeed;
- targetPos.x = Mathf.Clamp(targetPos.x, player.transform.position.x, mousePos.x);
- targetPos.y = Mathf.Clamp(targetPos.y, player.transform.position.y, mousePos.y);
- previousMousePos = mousePos;
- }
- // if()
- }
- void FixedUpdate()
- {
- // moveAmount = Vector3.Lerp(player.transform.position, (Camera.main.ScreenToWorldPoint(mousePos) + player.transform.position) / 2, smoothSpeed * Time.deltaTime);
- // Vector2 moveAmount = Vector2.Lerp((Vector2)player.transform.position, (new Vector2((mousePos.x + player.transform.position.x)/2, (mousePos.y + player.transform.position.y) / 2)), smoothSpeed * Time.deltaTime);
- // moveAmountFinal = new Vector3(moveAmount.x, moveAmount.y, -10);
- Camera.main.transform.position = Vector3.Lerp(player.transform.position, targetPos, smoothSpeed * Time.deltaTime);
- Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, -10);
- }
- */
- public GameObject player;
- // public float smoothSpeed = 8f;
- public float moveSpeed = 3f, camW, camH;
- public Vector3 targetPos, pastPos, pastTargetPos, positionToSetCameraTo, mousePos;
- private Vector2 offset = new Vector2((16f * 0.25f), (9f * 0.25f));
- private Camera cam;
- private void Start()
- {
- cam = Camera.main;
- camH = 2f * cam.orthographicSize;
- camW = camH * cam.aspect;
- targetPos = cam.transform.position;
- pastPos = cam.transform.position;
- pastTargetPos = cam.transform.position;
- }
- private void Update()
- {
- camH = 2f * cam.orthographicSize;
- camW = camH * cam.aspect;
- offset = new Vector2((16f * 0.01f * camW), (9f * 0.01f * camH));
- mousePos = Input.mousePosition;
- mousePos = cam.ScreenToWorldPoint(mousePos);
- // mousePos.x = Mathf.Clamp(mousePos.x, cam.transform.position.x - (camW / 2f) + 5f,cam.transform.position.x + (camW / 2f) - 5f);
- // mousePos.y = Mathf.Clamp(mousePos.y, cam.transform.position.y - (camH / 2f), cam.transform.position.y + (camH / 2f));
- targetPos = (player.transform.position + mousePos) / 2f;
- }
- private void FixedUpdate()
- {
- positionToSetCameraTo = SmoothApproach(pastPos, pastTargetPos, targetPos, moveSpeed);
- positionToSetCameraTo = new Vector3(positionToSetCameraTo.x, positionToSetCameraTo.y, -10f);
- positionToSetCameraTo.x = Mathf.Clamp(positionToSetCameraTo.x, player.transform.position.x - (camW / 2f) + offset.x, player.transform.position.x + (camW / 2f) - offset.x);
- positionToSetCameraTo.y = Mathf.Clamp(positionToSetCameraTo.y, player.transform.position.y - (camH / 2f) + offset.y, player.transform.position.y + (camH / 2f) - offset.y);
- // positionToSetCameraTo.x = Mathf.Clamp(positionToSetCameraTo.x, player.transform.position.x - camH, player.transform.position.x + camH);
- // positionToSetCameraTo.y = Mathf.Clamp(positionToSetCameraTo.y, player.transform.position.y - camW, player.transform.position.y + camW);
- pastPos = cam.transform.position;
- pastTargetPos = targetPos;
- cam.transform.position = positionToSetCameraTo;
- }
- Vector3 SmoothApproach(Vector3 pastPosition, Vector3 pastTargetPosition, Vector3 targetPosition, float speed)
- {
- float t = Time.deltaTime * speed;
- Vector3 v = (targetPosition - pastTargetPosition) / t;
- Vector3 f = pastPosition - pastTargetPosition + v;
- return targetPosition - v + f * Mathf.Exp(-t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement