Sniper Rifle

redhawkredhawk Posts: 489Member, MPTester
edited August 2015 in Scripts & Mods
Hello all. I'm back again with another video. This time, I made a sniper rifle video and am including the below script. I hope this is helpful to all. Also, please click some ads if you like the videos and want me to do more.

Nothing super fancy with the Script, but I actually like how well it works. Especially since I did not have to change any scripts that come with UFPS.

Here's the video

youtu.be/LgN0rMYtKSc?list=PLm7dDCs_gFwOWiIuLD4ScjgMm5wa6in1S

FYI - I just added the ability to Zoom in and out based on Mouse Scroll Wheel
NOTE = UNCOMMENT LINES 39 and 54 if you are using UFPS 1.5
using UnityEngine;
using System.Collections;

public class SniperScript : MonoBehaviour {

//DefaultCrosshair
public Texture DefaultCrosshair = null;//Default Crosshair - use UFPS
public Texture SniperCrosshair=null;//Sniper Crosshair - make your own
public float FieldOfViewSetting=9f;//Maxmium Zoom amount
public float fovMultiplier=2f;


protected vp_FPPlayerEventHandler m_Player = null;
protected vp_SimpleCrosshair m_crosshair=null;
protected vp_FPCamera m_camera=null;
Camera myCamera=null;

float DefaultFOV=60f;//Default FieldOfView that your weapon is set to
float maxFOV=60f;//Maximum FieldOfView will equal the DefaultFOV. You may want to cut this in half
public float tempFOV=0f;//Temporary holder for the current Field of View

protected virtual void Awake()
{

m_Player = GameObject.FindObjectOfType(typeof(vp_FPPlayerEventHandler)) as vp_FPPlayerEventHandler; // cache the player event handler
m_crosshair = GameObject.FindObjectOfType(typeof(vp_SimpleCrosshair)) as vp_SimpleCrosshair; // cache the Simple Crosshair
m_camera = GameObject.FindObjectOfType(typeof(vp_FPCamera)) as vp_FPCamera; // cache the Simple Crosshair
myCamera=m_camera.GetComponent<Camera>();
DefaultFOV=m_camera.RenderingFieldOfView;
maxFOV=DefaultFOV;

}

void OnGUI ()
{
if(m_Player.Zoom.Active)
{
m_Player.Crosshair.Set(SniperCrosshair);
// m_crosshair.HideOnFirstPersonZoom=false;//UNCOMMENT THIS FOR UFPS 1.5
tempFOV+=Input.GetAxis("Mouse ScrollWheel")*-fovMultiplier;//Assign temp zoom times -2. Scroll forward zoom in. Raise or lower -2 for quicker zooming
//Handle the Zooming using the Mouse ScrollWheel.

if(tempFOV>=maxFOV)//IF we have reached the Maximum, then don't zoom out anymore. Might want to cut it in half here
tempFOV=maxFOV;
else if(tempFOV<=FieldOfViewSetting)//If we reached closest zoom level, then stop zooming in
tempFOV=FieldOfViewSetting;
m_camera.RenderingFieldOfView=tempFOV;//Apply our Zoom amount
myCamera.fieldOfView=tempFOV;

}
else
{
m_crosshair.m_ImageCrosshair=DefaultCrosshair;
// m_crosshair.HideOnFirstPersonZoom=true; //UNCOMMENT THIS FOR UFPS 1.5
m_camera.RenderingFieldOfView=DefaultFOV;
}
}
}


Below is my Zoom Settings - basically the same as Pistol Zoom. Feel free to copy the other settings off the video at the end.
///////////////////////////////////////////////////////////
// Component Preset Script
///////////////////////////////////////////////////////////

ComponentType vp_FPWeapon
ShakeSpeed 0.05
ShakeAmplitude 0.5 0 0
PositionOffset 0 -0.58 0.17
RotationOffset 0.4917541 0.015994 0
PositionSpringStiffness 0.055
PositionSpringDamping 0.45
RotationSpringStiffness 0.025
RotationSpringDamping 0.35
RenderingFieldOfView 9
RenderingZoomDamping 0.2
BobAmplitude 0.5 0.4 0.2 0.005
BobRate 0.8 -0.4 0.4 0.4
BobInputVelocityScale 15
PositionWalkSlide 0.2 0.5 0.2


Comments

  • TryThisTryThis Posts: 887Member
    Wow! What an involved and detailed tutorial. That will help so many people see how to work so many different things. Academy Award stuff for sure. Very impressive!
  • redhawkredhawk Posts: 489Member, MPTester
    Script to work with UFPS 1.4.8. There is a new feature on the simple crosshair that references a boolean for hide on first person zoom. So this script grabs whatever is set, saves the setting, and sets it as false when zoom. Otherwise the crosshair doesn't show up.

    using UnityEngine;
    using System.Collections;

    public class SniperScript : MonoBehaviour {

    //DefaultCrosshair
    public Texture DefaultCrosshair = null;//Default Crosshair - use UFPS
    public Texture SniperCrosshair=null;//Sniper Crosshair - make your own
    public float FieldOfViewSetting=9f;//Maxmium Zoom amount
    public float fovMultiplier=2f;


    protected vp_FPPlayerEventHandler m_Player = null;
    protected vp_SimpleCrosshair m_crosshair=null;
    protected vp_FPCamera m_camera=null;
    Camera myCamera=null;

    float DefaultFOV=60f;//Default FieldOfView that your weapon is set to
    float maxFOV=60f;//Maximum FieldOfView will equal the DefaultFOV. You may want to cut this in half
    public float tempFOV=0f;//Temporary holder for the current Field of View
    bool hideZoom=true;
    bool resetZoom=false;

    protected virtual void Awake()
    {

    m_Player = GameObject.FindObjectOfType(typeof(vp_FPPlayerEventHandler)) as vp_FPPlayerEventHandler; // cache the player event handler
    m_crosshair = GameObject.FindObjectOfType(typeof(vp_SimpleCrosshair)) as vp_SimpleCrosshair; // cache the Simple Crosshair
    m_camera = GameObject.FindObjectOfType(typeof(vp_FPCamera)) as vp_FPCamera; // cache the Simple Crosshair
    myCamera=m_camera.GetComponent<Camera>();
    DefaultFOV=m_camera.RenderingFieldOfView;
    maxFOV=DefaultFOV;
    hideZoom=m_crosshair.HideOnFirstPersonZoom;

    }

    void OnGUI ()
    {
    if(m_Player.Zoom.Active)
    {
    m_crosshair.HideOnFirstPersonZoom=resetZoom;
    m_Player.Crosshair.Set(SniperCrosshair);
    tempFOV+=Input.GetAxis("Mouse ScrollWheel")*-fovMultiplier;//Assign temp zoom times -2. Scroll forward zoom in. Raise or lower -2 for quicker zooming
    //Handle the Zooming using the Mouse ScrollWheel.

    if(tempFOV>=maxFOV)//IF we have reached the Maximum, then don't zoom out anymore. Might want to cut it in half here
    tempFOV=maxFOV;
    else if(tempFOV<=FieldOfViewSetting)//If we reached closest zoom level, then stop zooming in
    tempFOV=FieldOfViewSetting;
    m_camera.RenderingFieldOfView=tempFOV;//Apply our Zoom amount
    myCamera.fieldOfView=tempFOV;

    }
    else
    {
    m_crosshair.HideOnFirstPersonZoom=hideZoom;
    m_crosshair.m_ImageCrosshair=DefaultCrosshair;
    m_camera.RenderingFieldOfView=DefaultFOV;
    }
    }
    }
  • radicalero Posts: 97Member, MPTester
  • Hellpatrol1488 Posts: 34Member, MPTester
    Works like a charm :-) Thanks

    Only one little thing: It would be great if the script disables the zoom during the reload state.
  • TryThisTryThis Posts: 887Member
    @Redhawk! What the hell! You didn't foresee every whim?! What's with that!? ha!!!
  • BrandSpankingNewBrandSpankingNew Posts: 2,321Member, MPTester
    @redhawk‌ that is a very nice tutorial. Here's some triple axel's for ya.
    Beautiful objects are wrought by study through effort, but ugly things are reaped automatically without toil.
    Democritus
  • redhawkredhawk Posts: 489Member, MPTester
  • pallavpanda Posts: 1Member
    Is there a way to make this work with the UFPS mobile controls?
  • fastbombfastbomb Posts: 368Member, MPTester
    @redhawk epic tutorial! I am glad that I finally got to this one! I tried to click on as many Ads as possible.

    All the best!
    Darryl Ashley | 3D Artist and Indie Dev
    Portfolio - fastbomb.com | Twitter - fastbomb
  • redhawkredhawk Posts: 489Member, MPTester
    fastbomb said:

    @redhawk epic tutorial! I am glad that I finally got to this one! I tried to click on as many Ads as possible.

    All the best!

    Cool, thanks. I haven't tested it in a while, so hopefully it all still works well with the latest version of UFPS. Sometimes updates make these older videos and scripts not work.

  • fastbombfastbomb Posts: 368Member, MPTester
    @redhawk got it working in UFPS and acParkour with very little effort. Thanks again!
    Darryl Ashley | 3D Artist and Indie Dev
    Portfolio - fastbomb.com | Twitter - fastbomb
  • redhawkredhawk Posts: 489Member, MPTester
    Nice. I haven't played around with my copy of acParkour lately.
  • AbyssAbyss Posts: 268Member, MPTester
    edited March 2015
    how would i modify the script so it won't show a reload while in the zoomed state ...
    or Break from the zoom state when the reload happens ...
  • sheffieldladsheffieldlad Posts: 126Member
    @Abyss use state blocking.
  • AbyssAbyss Posts: 268Member, MPTester
  • sheffieldladsheffieldlad Posts: 126Member
    @Abyss no code is needed. You do it in the editor.
    Take a look at the vp_FPWeapon script attached to the weapons which are child objects of your FP camnera.
    Click the states section. Each state has a button with the letter B on it. Click that and it will give you an option of which states that state should block.
    Breaking from zoom should probably happen if you set the reload to block the zoom state. (Possibly. This is untested. I'm not at my computer at the moment).

    Let us know how you get on :)
  • AbyssAbyss Posts: 268Member, MPTester
    ill try it today ....
  • AbyssAbyss Posts: 268Member, MPTester
    i tried it and yer I'm half way there ... i would like to have the zoom drop while the gun reloads ...

    can't work out how to stop the zoom when running the sniper script

  • fastbombfastbomb Posts: 368Member, MPTester
    @Abyss you might need to code something like this or your players will need to learn to zoom out.

    Blocking the State will make the Reload fail but I think you want it to force the player out of a Zoom State?
    Darryl Ashley | 3D Artist and Indie Dev
    Portfolio - fastbomb.com | Twitter - fastbomb
  • mareqq Posts: 3Member
    @redhawk I have question how can I increase the accuracy of shot? Everything works fine but accuracy is terrible.
  • redhawkredhawk Posts: 489Member, MPTester
    Play with the spread on the vp_FPWeaponShooter. Also you might want to increase the projectile's Range
  • mareqq Posts: 3Member
    edited June 2015
    @redhawk it's probably problem with zoom script, I change spread to 0 and without zoom works fine but with zoom accuracy is much worse. I tested other weapons and in zoom mode accuracy is also worse.
  • redhawkredhawk Posts: 489Member, MPTester
    http://opsive.com/assets/UFPS/forum/discussion/comment/10233/#Comment_10233

    This link is the updated stuff for 1.5. Maybe that's part of the problem. I personally had no issues with the set up, and had good accuracy. I cranked up the range, force, and set my spread to 1 so I would have a little deviation.
  • Pocheson Posts: 23Member
    redhawk said:

    http://opsive.com/assets/UFPS/forum/discussion/comment/10233/#Comment_10233

    This link is the updated stuff for 1.5. Maybe that's part of the problem. I personally had no issues with the set up, and had good accuracy. I cranked up the range, force, and set my spread to 1 so I would have a little deviation.

    Sniper Rifle PC version script works on 1.5?
    The script version 1.4.8 while reloading weapons I look in the sniper scope and I see as a weapon recharges.
  • mareqq Posts: 3Member
    @redhawk I have done another few test (set Sniper script, Sniper bullet and spread) one with Pistol and another with AssaultRifle and Machinegun - Pistol works fine but other weapons not, there is some config that broken accuracy.
  • redhawkredhawk Posts: 489Member, MPTester
    I will look at it soon. Not sure why you all are having issues. I had no problems when testing before I made the videos. I even have it working nicely with the Mobile add-on. Of course, I did crank up the force and range for my bullets.
  • redhawkredhawk Posts: 489Member, MPTester
    Updated the script. Works with UFPS version 1.5. Just uncomment lines 39 and 54
  • Pocheson Posts: 23Member
    edited March 2016
    redhawk said:

    Updated the script. Works with UFPS version 1.5. Just uncomment lines 39 and 54

    Ok, thanks
  • redhawkredhawk Posts: 489Member, MPTester
    Hey all, thought I would share. I simplified the script and registered it with the player. If you want to have an in-game adjustable scope, then I would suggest adding a new vp_Activity and looking for that like I do with OnStart_Zoom and OnStop_Zoom.

    This is more of a simplistic zoom where it goes to a specific setting when Zoomed in (changes vp_FPWeapon camera too). You could use this for any weapon that has a nice scope (like rocket launcher).


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    namespace RedHawk
    {
    public class Sniper : MonoBehaviour
    {

    public float FieldOfViewSetting = 9f;//Maxmium Zoom amount

    protected vp_FPPlayerEventHandler m_Player = null;
    protected vp_FPCamera m_camera = null;
    Camera myCamera = null;
    float DefaultFOV = 60f;//Default FieldOfView that your weapon is set to

    protected virtual void Awake()
    {
    m_Player = this.GetComponentInParent<vp_FPPlayerEventHandler>();
    m_camera = m_Player.GetComponentInChildren<vp_FPCamera>();
    myCamera = m_camera.GetComponent<Camera>();
    DefaultFOV = m_camera.RenderingFieldOfView;
    }
    #region Register Component with UFPS
    protected virtual void OnEnable()
    {
    if (m_Player != null)
    m_Player.Register(this);
    }
    protected virtual void OnDisable()
    {
    if (m_Player != null)
    m_Player.Unregister(this);
    //DO ZOOM OUT SETTINGS
    stopZoom();
    }
    #endregion

    protected void OnStart_Zoom()
    {
    //DO ZOOM IN SETTINGS
    startZoom();
    }
    protected void OnStop_Zoom()
    {
    //DO ZOOM OUT SETTINGS
    stopZoom();
    }
    //ZOOM IN SETTINGS
    public void startZoom()
    {
    m_camera.RenderingFieldOfView = FieldOfViewSetting;
    myCamera.fieldOfView = FieldOfViewSetting;
    }
    //ZOOM OUT SETTINGS
    public void stopZoom()
    {
    m_camera.RenderingFieldOfView = DefaultFOV;
    }
    }
    }
  • BrandSpankingNewBrandSpankingNew Posts: 2,321Member, MPTester
    Nicely done! This is a keeper!
    Beautiful objects are wrought by study through effort, but ugly things are reaped automatically without toil.
    Democritus
Sign In or Register to comment.