Advertisement
JJCUBER

Untitled

Dec 2nd, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraMovement : MonoBehaviour {
  6.     /*
  7.     public GameObject player;
  8.     public float smoothSpeed = 40f;
  9.     private Vector3 moveAmountFinal;
  10.     private Vector2 mousePos;
  11.     private Vector2 view;
  12.     private Vector2 moveAmount;
  13.     private bool isOutside;
  14.  
  15.     // Update is called once per frame
  16.     void FixedUpdate () {
  17.         /*Vector3 view = Camera.main.ScreenToViewportPoint(Input.mousePosition); // This works!!! It detects whether or not your mouse is outside of the game
  18.         bool isOutside = view.x < 0 || view.x > 1 || view.y < 0 || view.y > 1;
  19.         if (isOutside) print("OUTSIDE");* /
  20.  
  21.         mousePos = (Vector2)Input.mousePosition;
  22.  
  23.         view = (Vector2)Camera.main.ScreenToViewportPoint(mousePos); // This works!!! It detects whether or not your mouse is outside of the game
  24.         isOutside = view.x < 0 || view.x > 1 || view.y < 0 || view.y > 1;
  25.  
  26.         if (!isOutside)
  27.         {
  28.  
  29.             moveAmount = Vector2.Lerp((Vector2)player.transform.position, ((Vector2)Camera.main.ScreenToWorldPoint(mousePos) + (Vector2)player.transform.position) / 2, smoothSpeed * Time.deltaTime);
  30.             // 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);
  31.  
  32.             moveAmountFinal = new Vector3(moveAmount.x, moveAmount.y, -10);
  33.  
  34.             Camera.main.transform.position = moveAmountFinal;
  35.  
  36.         }
  37.        
  38.         // 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!
  39.         // 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!
  40.     }
  41.     */
  42.     /*
  43.     public GameObject player;
  44.     public float smoothSpeed = 8f;
  45.     public float moveSpeed = 3f;
  46.     public Vector3 targetPos;
  47.     public Vector3 previousMousePos;
  48.  
  49.     private void Start()
  50.     {
  51.         targetPos = Camera.main.transform.position;
  52.         previousMousePos = new Vector3(0, 0, 0);
  53.     }
  54.  
  55.     // Update is called once per frame
  56.     private void Update()
  57.     {
  58.         Vector3 mousePos = Input.mousePosition;
  59.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
  60.         Vector3 view = Camera.main.ScreenToViewportPoint(mousePos); // This works!!! It detects whether or not your mouse is outside of the game
  61.         bool outside = view.x < 0 || view.x > 1 || view.y < 0 || view.y > 1;
  62.         if (!outside && mousePos != previousMousePos)
  63.         {
  64.             Vector3 distance = ((Camera.main.transform.position + mousePos) / 2) - (Camera.main.transform.position); // This is the distance from the midpoint to the camera
  65.             // print(distance + " | " + moveSpeed + " | " + (distance * moveSpeed)); // Tests if multiplying a vector by a float multiplies all parts (x,y,z,etc.), and it does
  66.             targetPos -= distance * moveSpeed;
  67.             targetPos.x = Mathf.Clamp(targetPos.x, player.transform.position.x, mousePos.x);
  68.             targetPos.y = Mathf.Clamp(targetPos.y, player.transform.position.y, mousePos.y);
  69.             previousMousePos = mousePos;
  70.         }
  71.         // if()
  72.     }
  73.  
  74.     void FixedUpdate()
  75.     {
  76.         // moveAmount = Vector3.Lerp(player.transform.position, (Camera.main.ScreenToWorldPoint(mousePos) + player.transform.position) / 2, smoothSpeed * Time.deltaTime);
  77.         // 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);
  78.  
  79.         // moveAmountFinal = new Vector3(moveAmount.x, moveAmount.y, -10);
  80.  
  81.         Camera.main.transform.position = Vector3.Lerp(player.transform.position, targetPos, smoothSpeed * Time.deltaTime);
  82.         Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, -10);
  83.     }
  84.     */
  85.  
  86.     public GameObject player;
  87.     // public float smoothSpeed = 8f;
  88.     public float moveSpeed = 3f, camW, camH;
  89.     public Vector3 targetPos, pastPos, pastTargetPos, positionToSetCameraTo, mousePos;
  90.     private Vector2 offset = new Vector2((16f * 0.25f), (9f * 0.25f));
  91.     private Camera cam;
  92.  
  93.     private void Start()
  94.     {
  95.         cam = Camera.main;
  96.         camH = 2f * cam.orthographicSize;
  97.         camW = camH * cam.aspect;
  98.  
  99.         targetPos = cam.transform.position;
  100.         pastPos = cam.transform.position;
  101.         pastTargetPos = cam.transform.position;
  102.  
  103.     }
  104.  
  105.     private void Update()
  106.     {
  107.         camH = 2f * cam.orthographicSize;
  108.         camW = camH * cam.aspect;
  109.         offset = new Vector2((16f * 0.01f * camW), (9f * 0.01f * camH));
  110.         mousePos = Input.mousePosition;
  111.         mousePos = cam.ScreenToWorldPoint(mousePos);
  112.         // mousePos.x = Mathf.Clamp(mousePos.x, cam.transform.position.x - (camW / 2f) + 5f,cam.transform.position.x + (camW / 2f) - 5f);
  113.         // mousePos.y = Mathf.Clamp(mousePos.y, cam.transform.position.y - (camH / 2f), cam.transform.position.y + (camH / 2f));
  114.         targetPos = (player.transform.position + mousePos) / 2f;
  115.  
  116.     }
  117.  
  118.     private void FixedUpdate()
  119.     {
  120.  
  121.         positionToSetCameraTo = SmoothApproach(pastPos, pastTargetPos, targetPos, moveSpeed);
  122.         positionToSetCameraTo = new Vector3(positionToSetCameraTo.x, positionToSetCameraTo.y, -10f);
  123.         positionToSetCameraTo.x = Mathf.Clamp(positionToSetCameraTo.x, player.transform.position.x - (camW / 2f) + offset.x, player.transform.position.x + (camW / 2f) - offset.x);
  124.         positionToSetCameraTo.y = Mathf.Clamp(positionToSetCameraTo.y, player.transform.position.y - (camH / 2f) + offset.y, player.transform.position.y + (camH / 2f) - offset.y);
  125.         // positionToSetCameraTo.x = Mathf.Clamp(positionToSetCameraTo.x, player.transform.position.x - camH, player.transform.position.x + camH);
  126.         // positionToSetCameraTo.y = Mathf.Clamp(positionToSetCameraTo.y, player.transform.position.y - camW, player.transform.position.y + camW);
  127.         pastPos = cam.transform.position;
  128.         pastTargetPos = targetPos;
  129.         cam.transform.position = positionToSetCameraTo;
  130.     }
  131.  
  132.     Vector3 SmoothApproach(Vector3 pastPosition, Vector3 pastTargetPosition, Vector3 targetPosition, float speed)
  133.     {
  134.         float t = Time.deltaTime * speed;
  135.         Vector3 v = (targetPosition - pastTargetPosition) / t;
  136.         Vector3 f = pastPosition - pastTargetPosition + v;
  137.         return targetPosition - v + f * Mathf.Exp(-t);
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement