SHARE
TWEET

Freecam

Eddlm Apr 17th, 2016 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using GTA;
  2. using System;
  3. using System.Windows.Forms;
  4. using GTA.Math;
  5. using GTA.Native;
  6. using NativeUI;
  7.  
  8. public class ScriptTest : Script
  9. {
  10.     Camera RTSCam = World.CreateCamera(Game.Player.Character.Position, new Vector3(0, 0, 0), 60);
  11.     float smoothcam = 0.0f;
  12.     float smoothcamHoriz = 0.0f;
  13.     Entity EntToFollow;
  14.  
  15.     bool IsCursorOn = false;
  16.     int DesiredHeight = 10;
  17.     public ScriptTest()
  18.     {
  19.         Tick += OnTick;
  20.         KeyDown += OnKeyDown;
  21.         KeyUp += OnKeyUp;
  22.  
  23.     }
  24.  
  25.     void OnTick(object sender, EventArgs e)
  26.     {
  27.         if (smoothcam > 0.5f) smoothcam = 0.5f;
  28.         if (smoothcam < -0.5f) smoothcam = -0.5f;
  29.         if (smoothcamHoriz > 2) smoothcamHoriz = 2;
  30.         if (DesiredHeight < 3) DesiredHeight = 3;
  31.         if (DesiredHeight > 95) DesiredHeight = 95;
  32.  
  33.         //Math.Round(smoothcam, 1);
  34.         HandleRTSCam();
  35.  
  36.         if (Game.IsControlJustPressed(2, GTA.Control.FrontendAccept))
  37.         {
  38.             if (World.RenderingCamera == RTSCam)
  39.             {
  40.                 World.RenderingCamera = null;
  41.                 Game.Player.Character.Position = World.GetNextPositionOnSidewalk(Game.Player.Character.Position);
  42.             }
  43.             else
  44.             {
  45.                 World.RenderingCamera = RTSCam;
  46.             }
  47.         }
  48.     }
  49.     void OnKeyDown(object sender, KeyEventArgs e)
  50.     {
  51.  
  52.     }
  53.     void OnKeyUp(object sender, KeyEventArgs e)
  54.     {
  55.  
  56.     }
  57.  
  58.     void HandleRTSCam()
  59.     {
  60.  
  61.         if (World.RenderingCamera == RTSCam)
  62.         {
  63.  
  64.             DisplayHelpTextThisFrame("Full Freecam: ~INPUT_JUMP~~n~Faster Cam: ~INPUT_SPRINT~~n~Raise Cam: ~INPUT_SELECT_PREV_WEAPON~");
  65.  
  66.             Function.Call(Hash.HIDE_HUD_AND_RADAR_THIS_FRAME);
  67.             Function.Call(Hash.DISABLE_ALL_CONTROL_ACTIONS, 0);
  68.             Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.LookLeftRight);
  69.             Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.LookUpDown);
  70.             Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.CursorX);
  71.             Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.CursorY);
  72.             Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.FrontendPauseAlternate);
  73.             Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.WeaponWheelPrev);
  74.             Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.WeaponWheelNext);
  75.             Game.Player.Character.Position = RTSCam.Position - RotationToDirection(RTSCam.Rotation) * 8f;
  76.  
  77.             if (Game.IsKeyPressed(Keys.ControlKey))
  78.             {
  79.                 Function.Call(Hash._SHOW_CURSOR_THIS_FRAME);
  80.                 if (!IsCursorOn) IsCursorOn = true;
  81.                 if (Game.IsControlJustPressed(2, GTA.Control.Aim))
  82.                 {
  83.                     Vector2 mousecoords = new Vector2(Cursor.Position.X, Cursor.Position.Y);
  84.                     mousecoords = new Vector2((mousecoords.X / UIMenu.GetScreenResolutionMantainRatio().Width) * 2 - 1, (mousecoords.Y / UIMenu.GetScreenResolutionMantainRatio().Height) * 2 - 1);
  85.                     UI.Notify(mousecoords.ToString());
  86.  
  87.                     Entity ent = RaycastEverything(mousecoords);
  88.                     if (CanWeUse(ent))
  89.                     {
  90.                         EntToFollow = ent;
  91.                         //RTSCam.AttachTo(ent, EntityOffsetGivenWorldCoords(ent, ent.Position + (ent.ForwardVector * -10) + ent.UpVector * 5));
  92.                         RTSCam.Rotation = new Vector3(0, 0, ent.Rotation.Z);
  93.                     }
  94.                     else
  95.                     {
  96.                         UI.Notify("~r~Couldn't find entity.");
  97.                     }
  98.                 }
  99.             }
  100.             else
  101.             {
  102.  
  103.                 float mouseX = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)GTA.Control.LookLeftRight);
  104.                 float mouseY = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)GTA.Control.LookUpDown);
  105.                 RTSCam.Rotation = RTSCam.Rotation + new Vector3(mouseY * -5, 0, mouseX * -5);
  106.                 if (RTSCam.Rotation.X < -90) RTSCam.Rotation = RTSCam.Rotation + new Vector3(1, 0, 0);
  107.                 if (RTSCam.Rotation.X > 90) RTSCam.Rotation = RTSCam.Rotation + new Vector3(-1, 0, 0);
  108.  
  109.                 if (IsCursorOn) IsCursorOn = false;
  110.  
  111.                 if (CanWeUse(EntToFollow))
  112.                 {
  113.                     //RTSCam.Position = EntToFollow.Position - new Vector3(EntToFollow.Velocity.X, EntToFollow.Velocity.Y,5*-1) + (EntToFollow.UpVector*3);
  114.                     RTSCam.Position = EntToFollow.Position+(new Vector3(0,0,EntToFollow.Model.GetDimensions().Z*2));
  115.                     if (Game.IsControlJustPressed(2, GTA.Control.Aim))
  116.                     {
  117.                         EntToFollow = null;
  118.                     }
  119.                 }
  120.                 else
  121.                 {
  122.                     bool ShouldFixCameraHeight = !Game.IsControlPressed(2, GTA.Control.Jump);
  123.                     float modifier = 1f;
  124.                     if (Game.IsControlPressed(2, GTA.Control.Sprint)) modifier = 3f;
  125.                     if (ShouldFixCameraHeight)
  126.                     {
  127.                         RaycastResult result = World.Raycast(RTSCam.Position, new Vector3(0, 0, -1), 100f, IntersectOptions.Map, Game.Player.Character);
  128.                         if (RTSCam.Position.Z - result.HitCoords.Z < DesiredHeight - 2f)
  129.                         {
  130.                             smoothcam = smoothcam + 0.05f;
  131.                         }
  132.                         else if (RTSCam.Position.Z - result.HitCoords.Z > DesiredHeight)
  133.                         {
  134.                             smoothcam = smoothcam - 0.05f;
  135.                         }
  136.                         else if (smoothcam > 0)
  137.                         {
  138.                             smoothcam = smoothcam - 0.05f;
  139.                         }
  140.                         else if (smoothcam < 0)
  141.                         {
  142.                             smoothcam = smoothcam + 0.05f;
  143.                         }
  144.                         smoothcam = (float)Math.Round(smoothcam, 2);
  145.                     }
  146.                     else
  147.                     {
  148.                         if (smoothcam != 0) smoothcam = 0;
  149.                     }
  150.  
  151.  
  152.                     Vector3 newpos = RTSCam.Position;
  153.                     Vector3 newdir = RotationToDirection(RTSCam.Rotation);
  154.  
  155.                     float _Z = RTSCam.Position.Z;
  156.  
  157.                     var rotLeft = RTSCam.Rotation + new Vector3(0, 0, -10);
  158.                     var rotRight = RTSCam.Rotation + new Vector3(0, 0, 10);
  159.                     var right = RotationToDirection(rotRight) - RotationToDirection(rotLeft);
  160.                     if (Game.IsControlPressed(0, GTA.Control.MoveUpOnly))
  161.                     {
  162.                         newpos += newdir * modifier;
  163.                     }
  164.                     if (Game.IsControlPressed(0, GTA.Control.MoveDownOnly))
  165.                     {
  166.                         newpos -= newdir * modifier;
  167.                     }
  168.                     if (Game.IsControlPressed(0, GTA.Control.MoveLeftOnly))
  169.                     {
  170.                         newpos += right * (modifier * 2);
  171.                     }
  172.                     if (Game.IsControlPressed(0, GTA.Control.MoveRightOnly))
  173.                     {
  174.                         newpos -= right * (modifier * 2);
  175.                     }
  176.  
  177.                     if (Game.IsControlPressed(2, GTA.Control.WeaponWheelPrev))
  178.                     {
  179.                         if (Game.IsControlPressed(2, GTA.Control.Sprint)) DesiredHeight += 5; else DesiredHeight += 1;
  180.                     }
  181.                     if (Game.IsControlPressed(2, GTA.Control.WeaponWheelNext))
  182.                     {
  183.                         if (Game.IsControlPressed(2, GTA.Control.Sprint)) DesiredHeight -= 5; else DesiredHeight -= 1;
  184.                     }
  185.                     if (!ShouldFixCameraHeight) RTSCam.Position = newpos; else RTSCam.Position = new Vector3(newpos.X, newpos.Y, _Z + smoothcam);
  186.                 }
  187.             }
  188.         }
  189.         else if (CanWeUse(EntToFollow)) EntToFollow = null; //RTSCam.Detach();
  190.  
  191.     }
  192.     Vector3 EntityOffsetGivenWorldCoords(Entity ent, Vector3 coords)
  193.     {
  194.         return Function.Call<Vector3>(Hash.GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS, ent, coords.X,coords.Y,coords.Z);
  195.     }
  196.  
  197.     bool CanWeUse(Entity entity)
  198.     {
  199.         return entity != null && entity.Exists();
  200.     }
  201.     protected override void Dispose(bool dispose)
  202.     {
  203.         World.RenderingCamera = null;
  204.         RTSCam.Destroy();
  205.         Game.Player.Character.Position = World.GetNextPositionOnSidewalk(Game.Player.Character.Position);
  206.  
  207.         base.Dispose(dispose);
  208.     }
  209.     void DisplayHelpTextThisFrame(string text)
  210.     {
  211.         Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
  212.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
  213.         Function.Call(Hash._DISPLAY_HELP_TEXT_FROM_STRING_LABEL, 0, 0, 0, -1);
  214.     }
  215.  
  216.     public Entity RaycastEverything(Vector2 screenCoord)
  217.     {
  218.         Vector3 camPos = RTSCam.Position;
  219.         Vector3 camRot = RTSCam.Rotation;
  220.         const float raycastToDist = 100.0f;
  221.         const float raycastFromDist = 1f;
  222.  
  223.         var target3D = ScreenRelToWorld(camPos, camRot, screenCoord);
  224.         var source3D = camPos;
  225.  
  226.         Entity ignoreEntity = Game.Player.Character;
  227.         if (Game.Player.Character.IsInVehicle())
  228.         {
  229.             ignoreEntity = Game.Player.Character.CurrentVehicle;
  230.         }
  231.  
  232.         var dir = (target3D - source3D);
  233.         dir.Normalize();
  234.         var raycastResults = World.Raycast(source3D + dir * raycastFromDist,
  235.             source3D + dir * raycastToDist,IntersectOptions.Everything, ignoreEntity);
  236.  
  237.         if (raycastResults.DitHitEntity)
  238.         {
  239.             return raycastResults.HitEntity;
  240.         }
  241.  
  242.         return null; //camPos + dir * raycastToDist;
  243.     }
  244.  
  245.     public static Vector3 ScreenRelToWorld(Vector3 camPos, Vector3 camRot, Vector2 coord)
  246.     {
  247.         var camForward = RotationToDirection(camRot);
  248.         var rotUp = camRot + new Vector3(10, 0, 0);
  249.         var rotDown = camRot + new Vector3(-10, 0, 0);
  250.         var rotLeft = camRot + new Vector3(0, 0, -10);
  251.         var rotRight = camRot + new Vector3(0, 0, 10);
  252.  
  253.         var camRight = RotationToDirection(rotRight) - RotationToDirection(rotLeft);
  254.         var camUp = RotationToDirection(rotUp) - RotationToDirection(rotDown);
  255.  
  256.         var rollRad = -DegToRad(camRot.Y);
  257.  
  258.         var camRightRoll = camRight * (float)Math.Cos(rollRad) - camUp * (float)Math.Sin(rollRad);
  259.         var camUpRoll = camRight * (float)Math.Sin(rollRad) + camUp * (float)Math.Cos(rollRad);
  260.  
  261.         var point3D = camPos + camForward * 10.0f + camRightRoll + camUpRoll;
  262.         Vector2 point2D;
  263.         if (!WorldToScreenRel(point3D, out point2D)) return camPos + camForward * 10.0f;
  264.         var point3DZero = camPos + camForward * 10.0f;
  265.         Vector2 point2DZero;
  266.         if (!WorldToScreenRel(point3DZero, out point2DZero)) return camPos + camForward * 10.0f;
  267.  
  268.         const double eps = 0.001;
  269.         if (Math.Abs(point2D.X - point2DZero.X) < eps || Math.Abs(point2D.Y - point2DZero.Y) < eps) return camPos + camForward * 10.0f;
  270.         var scaleX = (coord.X - point2DZero.X) / (point2D.X - point2DZero.X);
  271.         var scaleY = (coord.Y - point2DZero.Y) / (point2D.Y - point2DZero.Y);
  272.         var point3Dret = camPos + camForward * 10.0f + camRightRoll * scaleX + camUpRoll * scaleY;
  273.         return point3Dret;
  274.     }
  275.     public static bool WorldToScreenRel(Vector3 worldCoords, out Vector2 screenCoords)
  276.     {
  277.         OutputArgument num1 = new OutputArgument();
  278.         OutputArgument num2 = new OutputArgument();
  279.         if (!Function.Call<bool>(Hash._WORLD3D_TO_SCREEN2D, worldCoords.X, worldCoords.Y, worldCoords.Z, num1, num2))
  280.         {
  281.             screenCoords = new Vector2();
  282.             return false;
  283.         }
  284.         screenCoords = new Vector2((num1.GetResult<float>() - 0.5f) * 2, (num2.GetResult<float>() - 0.5f) * 2);
  285.         return true;
  286.     }
  287.     public static double DegToRad(double deg)
  288.     {
  289.         return deg * Math.PI / 180.0;
  290.     }
  291.     public static double RadToDeg(double deg)
  292.     {
  293.         return deg * 180.0 / Math.PI;
  294.     }
  295.     public static Vector3 RotationToDirection(Vector3 rotation)
  296.     {
  297.         var z = DegToRad(rotation.Z);
  298.         var x = DegToRad(rotation.X);
  299.         var num = Math.Abs(Math.Cos(x));
  300.         return new Vector3
  301.         {
  302.             X = (float)(-Math.Sin(z) * num),
  303.             Y = (float)(Math.Cos(z) * num),
  304.             Z = (float)Math.Sin(x)
  305.         };
  306.     }
  307. }
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