Advertisement
Guest User

FlyCam for Unity3D (Javascript)

a guest
Oct 18th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3.     /*Writen by Windexglow 11-13-10.  Use it, edit it, steal it I don't care.  
  4.     Simple flycam I made, since I couldn't find any others made public.  
  5.     Made simple to use (drag and drop, done) for regular keyboard layout  
  6.     wasd : basic movement
  7.     shift : Makes camera accelerate
  8.     left shift : Moves camera on X and Z axis only.  So camera doesn't gain any height
  9.     space : Moves camera straight up
  10.     X : Moves camera straight down*/
  11.    
  12.     var mainSpeed : float = 50.0; //regular speed
  13.     var shiftAdd : float = 250.0; //multiplied by how long shift is held.  Basically running
  14.     var maxShift : float = 1000.0; //Maximum speed when holdin gshift
  15.     var camSens : float = 0.25; //How sensitive it with mouse
  16.     private var lastMouse = Vector3(255, 255, 255); //kind of in the middle of the screen, rather than at the top (play)
  17.     private var totalRun : float  = 1.0;
  18.  
  19.     function Update ()
  20.     {
  21.         lastMouse = Input.mousePosition - lastMouse;
  22.         lastMouse = Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0 );
  23.         lastMouse = Vector3(transform.eulerAngles.x + lastMouse.x , transform.eulerAngles.y + lastMouse.y, 0);
  24.         transform.eulerAngles = lastMouse;
  25.         lastMouse =  Input.mousePosition;
  26.         //Mouse & camera angle done.  
  27.  
  28.    
  29.         //Keyboard commands
  30.         var f : float = 0.0;
  31.         var p = GetBaseInput();
  32.         if (Input.GetKey (KeyCode.LeftShift))
  33.         {
  34.             totalRun += Time.deltaTime;
  35.             p  = p * totalRun * shiftAdd;
  36.             p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
  37.             p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
  38.             p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
  39.         }
  40.         else
  41.         {
  42.             totalRun = Mathf.Clamp(totalRun * 0.5, 1, 1000);
  43.             p = p * mainSpeed;
  44.         }
  45.  
  46.         p = p * Time.deltaTime;
  47.         if (Input.GetKey(KeyCode.LeftShift))
  48.         {
  49.             //If player wants to move on X and Z axis only
  50.             f = transform.position.y;
  51.             transform.Translate(p);
  52.             transform.position.y = f;
  53.         }
  54.         else
  55.         {
  56.             transform.Translate( p);
  57.         }
  58.  
  59.     }
  60.  
  61.  
  62.  
  63.     private function GetBaseInput() : Vector3
  64.     {
  65.         //returns the basic values, if it's 0 than it's not active.
  66.         var p_Velocity : Vector3;
  67.         if (Input.GetKey (KeyCode.W))
  68.         {
  69.             p_Velocity += Vector3(0, 0 , 1);
  70.         }
  71.         if (Input.GetKey (KeyCode.S))
  72.         {
  73.             p_Velocity += Vector3(0, 0 , -1);
  74.         }
  75.         if (Input.GetKey (KeyCode.A))
  76.         {
  77.             p_Velocity += Vector3(-1, 0 , 0);
  78.         }
  79.         if (Input.GetKey (KeyCode.D))
  80.         {
  81.             p_Velocity += Vector3(1, 0 , 0);
  82.         }
  83.         if (Input.GetKey (KeyCode.Space))
  84.         {
  85.             p_Velocity += Vector3(0,1,0);
  86.         }
  87.         if (Input.GetKey (KeyCode.X))
  88.         {
  89.             p_Velocity += Vector3(0,-1,0);
  90.         }
  91.         return p_Velocity;
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement