Advertisement
shadowplaycoding

P007_CameraController

Apr 29th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraController : MonoBehaviour {
  5.  
  6.     public float panSpeed = 100;
  7.  
  8.     Transform rotationObject;
  9.     Transform zoomObject;
  10.  
  11.     // Used before Start()
  12.     void Awake()
  13.     {
  14.         rotationObject = transform.GetChild(0);
  15.         zoomObject = rotationObject.transform.GetChild(0);
  16.         ResetCamera();
  17.     }
  18.  
  19.     // Update is called once per frame
  20.     void Update() {
  21.  
  22.         ChangePosition();
  23.  
  24.     }
  25.  
  26.     // This method pans the camera view in the XZ plane
  27.     void ChangePosition()
  28.     {
  29.         if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
  30.         {
  31.  
  32.             float distance = panSpeed * Time.deltaTime;
  33.             Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
  34.  
  35.             float dampingFactor = Mathf.Max(Mathf.Abs(Input.GetAxis("Horizontal")), Mathf.Abs(Input.GetAxis("Vertical")));
  36.  
  37.             transform.Translate(distance * dampingFactor * direction);
  38.  
  39.         }
  40.     }
  41.  
  42.     // This method resets the camera to the centre of the scene
  43.     public void ResetCamera()
  44.     {
  45.         this.transform.position = new Vector3(0, 0, 0);
  46.     }
  47.  
  48.  
  49.     /*
  50.     Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  51.     Removing this comment forfits any rights given to the user under licensing.
  52.     */
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement