Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jake 3 · Oct 22, 2010 at 01:00 PM · gunfireaimdownsights

aim down sights of gun

i found this script on the unity wiki page,

how do i change the position of my gun?

im a beginner at scripting

var gun : Transform; var nextPos = 0.0; var nextField = 40.0; var nextPos2 = -0.2; var dampVelocity = 0.4; var dampVelocity2 = 0.4; var dampVelocity3 = 0.4;

function Update () { var newPos = Mathf.SmoothDamp(gun.transform.localPosition.x, nextPos, dampVelocity, .3); var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, nextField, dampVelocity2, .3); var newPos2 = Mathf.SmoothDamp(gun.transform.localPosition.y, nextPos2, dampVelocity3, .3);

gun.transform.localPosition.x = newPos; gun.transform.localPosition.y = newPos2; Camera.main.fieldOfView = newField;

if (Input.GetButton("Fire2")) { //adjust viewpoint and gun position nextField = 40.0; nextPos = 0.0; nextPos2 = -0.2;

   //slow down turning and movement speed
   GetComponent(FPSWalker).speed = 1.5;
   GetComponent("MouseLook").sensitivityX = 2;
   camera.main.GetComponent("MouseLook").sensitivityX = 2;
   camera.main.GetComponent("MouseLook").sensitivityY = 2;

} else { //adjust viewpoint and gun position nextField = 60.0; nextPos = 0.5; nextPos2 = -0.4;

//speed up turning and movement speed GetComponent(FPSWalker).speed = 6; GetComponent("MouseLook").sensitivityX = 6; camera.main.GetComponent("MouseLook").sensitivityX = 6; camera.main.GetComponent("MouseLook").sensitivityY = 6;

} }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image superpentil · Aug 05, 2012 at 11:44 PM 0
Share

You can change the position with the NextPos and NextPos2 varaibles. NextPos is the X position, and NextPos2 is the Y.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by superpentil · Aug 06, 2012 at 02:01 AM

have the gun model set to .4 for X and -.5 for Y, (you can have it at whatever z position you want) when its attatched to the camera. Then to test, just right click. To change the position of the gun (becuase when i use this script, it goes too far to the left of the screen) you can when the game isnt running find your guns best position by dragging the green and red arrows for position, to a place where its like your aiming down the sights in game. Copy the X and Y positions on a piece of paper, then in script you need to change the NextPos and NextPos2 variables under the if statement to the X and Y. (NextPos is the X and NextPos2 is the Y). Save and that should work. It may wiggle a little to the left, so try and compinsate for that.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by moinchdog · Oct 29, 2012 at 09:38 PM

try my re-write of the script. It also allows disabling of the CrossHair when aiming and it changes weapon Stats. Hope you like it. Give credit were its due.

Dj

 var gun : Transform; //Drag you Gun transform into This
  var curField = 60.0;
  private var dampVelocity2 = 0.4;
  var nextVector : Vector3; //Sights Transform
  var curVector : Vector3; //Normal Transform
  
  var curY = 234.8665; //Normal Rotation of Gun
  var nextY = 230.7; //Rotation of Gun when looking down Sights
 
  
  class weaponStats {
      var curDamage : int; //Normal Weapon Damage
      var nextDamage : int; //Sights Weapon Damage
      var gunScript : String;
      var mainCam : CrossHair; //Change "CrossHair" to the name of the Script or GUITexture you use for your CrossHair
  }
  var speed : float = 0.1;
  var stats : weaponStats;
  
  function Update () {
      //Smooth Changine Vield of View of Camera
     var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, curField, dampVelocity2, .3);
     Camera.main.fieldOfView = newField;
  
     if (Input.GetButton("Fire2")) { //"Fire2" is right click, can be changed
         //adjust viewpoint and gun position
         curField = 40;
         //Rotating Gun to look down Camera Perfectly
         transform.localRotation.eulerAngles = Vector3(0,nextY,0);
  
         //slow down turning and movement speed
         transform.root.GetComponent(CharacterMotor).movement.maxForwardSpeed = 2.5;
         //Adjust Gun Damage
         GetComponent(stats.gunScript).damage = stats.nextDamage;
         stats.mainCam.enabled = false;
         //Moving the gun
         transform.localPosition = Vector3.Lerp(transform.localPosition,nextVector, 0.3);
     } else {
         //adjust viewpoint and gun position
         curField = 60.0;
         transform.localRotation.eulerAngles = Vector3(0,curY,0);
         //speed up turning and movement speed
         transform.root.GetComponent(CharacterMotor).movement.maxForwardSpeed = 10;
         stats.mainCam.enabled = true;
         GetComponent(stats.gunScript).damage = stats.curDamage;
         transform.localPosition = Vector3.Lerp(transform.localPosition,curVector, speed);
     }
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tom1103 · Oct 30, 2012 at 11:56 AM

Thanks! :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nickman99 · Sep 23, 2013 at 09:44 AM 0
Share

Put a new camera above your characters wrist and position the camera to your ads preference (directly down sights/slightly up+left/etc) then add mouse movement script to camera and add a camera switcher script and assign original cam to cam 1 and the new one to cam 2 and to switch them in play mode simply press t button (with enough tampering you could switch the input to right mouse button

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

free aim down sights script here 0 Answers

Aimdown Sights Toggle 1 Answer

How do you aim down sights 1 Answer

gun fire p90`s how to spawn gun fire 2 Answers

Avert Fate Style FPS Gun Script 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges