SHARE
TWEET

KSP Mastcam - Source Code

a guest Dec 31st, 2015 149 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using KSP;
  8. namespace Mastcam
  9. {
  10.  
  11.     public class mastcam : PartModule
  12.     {
  13.        
  14.         private GameObject mountB;
  15.         private GameObject mastCamera;
  16.         private GameObject cam;
  17.         private float[] rotation = new float[2];
  18.         private FlightCamera flightCam;
  19.         private bool camActive;
  20.  
  21.         [KSPField(isPersistant = false)]
  22.         public float horizontalSensitivity;
  23.         [KSPField(isPersistant = false)]
  24.         public float verticalSensitivity;
  25.        
  26.         public void Start()
  27.         {
  28.             mountB = GameObject.Find("mountB");
  29.             mastCamera = GameObject.Find("mastCamParent");
  30.             cam = GameObject.Find("mastCamera");
  31.             flightCam = FlightCamera.fetch;
  32.             camActive = false;
  33.         }
  34.        
  35.         public void Update()
  36.         {
  37.             if (HighLogic.LoadedSceneIsFlight)
  38.             {
  39.                 camActive = !flightCam.updateActive;
  40.                 if (camActive) activateCamera();
  41.                 if (!camActive) deactivateCamera();
  42.                 if (camActive == true)
  43.                 {
  44.  
  45.                     if (Input.GetKey(KeyCode.LeftArrow)) rotation[0] -= horizontalSensitivity;
  46.                     if (Input.GetKey(KeyCode.RightArrow)) rotation[0] += horizontalSensitivity;
  47.                     if (Input.GetKey(KeyCode.UpArrow)) rotation[1] -= verticalSensitivity;
  48.                     if (Input.GetKey(KeyCode.DownArrow)) rotation[1] += verticalSensitivity;
  49.                     rotation[1] = Mathf.Clamp(rotation[1], -80, 80);
  50.  
  51.  
  52.                     if (mountB != null) mountB.transform.localEulerAngles = new Vector3(0, 0, rotation[0]);
  53.                     else
  54.                         Debug.Log("no mountB");
  55.                     if (mastCamera != null) mastCamera.transform.localEulerAngles = new Vector3(0, rotation[1], 0);
  56.                     else
  57.                         Debug.Log("no mastCamera");
  58.  
  59.                     flightCam.transform.position = cam.transform.position;
  60.                     flightCam.transform.rotation = cam.transform.rotation;
  61.                 }
  62.             }
  63.         }
  64.        
  65.         [KSPEvent(active = true, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Activate Camera")]
  66.         public void activateCamera()
  67.         {
  68.             flightCam.DeactivateUpdate();
  69.             Events["activateCamera"].active = false;
  70.             Events["deactivateCamera"].active = true;
  71.         }
  72.  
  73.         [KSPEvent(active = false, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Deactivate Camera")]
  74.         public void deactivateCamera()
  75.         {
  76.             flightCam.ActivateUpdate();
  77.             Events["activateCamera"].active = true;
  78.             Events["deactivateCamera"].active = false;
  79.         }
  80.  
  81.         [KSPAction("Activate Camera")]
  82.         public void activateCamera(KSPActionParam param)
  83.         {
  84.             activateCamera();
  85.             Actions["activateCamera"].active = false;
  86.             Actions["deactivateCamera"].active = true;
  87.         }
  88.  
  89.         [KSPAction("Deactivate Camera")]
  90.         public void deactivateCamera(KSPActionParam param)
  91.         {
  92.             deactivateCamera();
  93.             Actions["activateCamera"].active = true;
  94.             Actions["deactivateCamera"].active = false;
  95.         }
  96.    
  97.     }
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top