SHARE
TWEET

UberCam

MoMadenU Dec 29th, 2015 (edited) 511 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // This is the Uber Camera.  
  3. // Works just like Gameplay camera with added features of
  4. //
  5. // * Following any Entity
  6. // * No ground clamp
  7. // * Follow distance controlled by mouse wheel
  8. //
  9. // To exit demo press E
  10. //
  11. // You are free to use this code however you want, all I ask is
  12. // to be credited if you use it in a mod you have published
  13. //  -- MoMadenU  
  14. //
  15.  
  16. using System;
  17. using System.Drawing;
  18. using System.Windows.Forms;
  19. using GTA;
  20. using GTA.Math;
  21. using GTA.Native;
  22. using Control = GTA.Control;
  23.  
  24. namespace MoTools
  25. {
  26.     public class UberCamera
  27.     {
  28.         private static float _polarAngleDeg = 0;
  29.         private static float _azimuthAngleDeg = 90;
  30.         private static float _radius = 3.5f;
  31.  
  32.         public static void Demo()
  33.         {
  34.             var playerPed = Game.Player.Character;
  35.             var playerPedPos = playerPed.Position;
  36.  
  37.             var ped = World.CreatePed(new Model(42647445), new Vector3(playerPedPos.X, playerPedPos.Y, playerPedPos.Z + 100f));
  38.             ped.FreezePosition = true;
  39.  
  40.             Start(ped);
  41.  
  42.             while (!Game.IsKeyPressed(Keys.E))
  43.             {
  44.                 OnTick(ped);
  45.             }
  46.  
  47.             Stop();
  48.             ped.Delete();
  49.  
  50.         }
  51.  
  52.         public static void Start(Entity entity)
  53.         {
  54.             Stop();
  55.             World.RenderingCamera = World.CreateCamera(entity.Position, Vector3.Zero, 60f);
  56.         }
  57.  
  58.         public static void OnTick(Entity entity)
  59.         {
  60.             disableAllControls();
  61.  
  62.             if (Game.IsControlPressed(2, Control.CursorScrollDown) || Game.IsControlPressed(2, Control.MoveDownOnly))
  63.                 _radius += 0.5f;
  64.  
  65.             if (Game.IsControlPressed(2, Control.CursorScrollUp) || Game.IsControlPressed(2, Control.MoveUpOnly))
  66.                 _radius -= 0.5f;
  67.  
  68.             if (_radius < 1)
  69.                 _radius = 1;
  70.  
  71.             var xMagnitude = Function.Call<float>(Hash.GET_DISABLED_CONTROL_NORMAL, 0, (int)GTA.Control.LookLeftRight);
  72.             var yMagnitude = Function.Call<float>(Hash.GET_DISABLED_CONTROL_NORMAL, 0, (int)GTA.Control.LookUpDown);
  73.  
  74.             _polarAngleDeg += (xMagnitude * 10);
  75.  
  76.             if (_polarAngleDeg >= 360)
  77.                 _polarAngleDeg = 0;
  78.  
  79.             _azimuthAngleDeg += (yMagnitude * 10);
  80.  
  81.             if (_azimuthAngleDeg >= 360)
  82.                 _azimuthAngleDeg = 0;
  83.  
  84.             var nextCamLocation = polar3DToWorld3D(entity.Position, _radius, _polarAngleDeg, _azimuthAngleDeg);
  85.             World.RenderingCamera.Position = new Vector3(nextCamLocation.X, nextCamLocation.Y, nextCamLocation.Z);
  86.             World.RenderingCamera.PointAt(entity);
  87.  
  88.             Script.Wait(0);
  89.         }
  90.  
  91.         public static void Stop()
  92.         {
  93.             World.DestroyAllCameras();
  94.             World.RenderingCamera = null;
  95.         }
  96.  
  97.         private static Vector3 polar3DToWorld3D(Vector3 entityPosition, float radius, float polarAngleDeg, float azimuthAngleDeg)
  98.         {
  99.             // convert degrees to radians
  100.             var polarAngleRad = polarAngleDeg * Math.PI / 180.0;
  101.             var azimuthAngleRad = azimuthAngleDeg * Math.PI / 180.0;
  102.  
  103.             float x = entityPosition.X + radius * (float)(Math.Sin(azimuthAngleRad) * Math.Cos(polarAngleRad));
  104.             float y = entityPosition.Y - radius * (float)(Math.Sin(azimuthAngleRad) * Math.Sin(polarAngleRad));
  105.             float z = entityPosition.Z - radius * (float)(Math.Cos(azimuthAngleRad));
  106.  
  107.             return new Vector3(x, y, z);
  108.         }
  109.  
  110.         private static void disableAllControls()
  111.         {
  112.             foreach (Control control in Enum.GetValues(typeof(Control)))
  113.             {
  114.                 Function.Call(Hash.DISABLE_CONTROL_ACTION, 0, (int)control, 1);
  115.             }
  116.         }
  117.  
  118.         private static void enableAllControls()
  119.         {
  120.             foreach (Control control in Enum.GetValues(typeof(Control)))
  121.             {
  122.                 Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int) control, 1);
  123.             }
  124.         }
  125.     }
  126. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top