Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by · Aug 11, 2017 at 07:20 AM · positionvector3sprintobject movement

How to change the gun from Position A to B and again B to A ?

I am using Unity 5.6.1 I have model of gun and I want to do that, if I press the Right Shift Key the Gun position get change to Vector X, Y and Z and as I release the key the Gun again smoothly comes to it`s initial position! Is there anyone who have solution for my problem. It`s urgent ! :) Thanks in advance ! Sorry for Messy English !

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by TrainWrek · Aug 09, 2017 at 06:33 AM

You could try to modify the gun position over time.

 public float lerpSpeed = 1.5f;
 public Transform gun;
 public Vector3 onKeyPos;
 
 Vector3 origPos;
 
 void Start() {
     if (gun == null) {
         Debug.LogWarning('Gun transform is null, the component has been disabled.');
         enabled = false;
         return;
     }
     origPos = gun.transform.position;
 }
 
 void Update() {
     if (Input.GetKey(KeyCode.RightShift)) { //Move the gun towards the onKeyPos
         gun.position = Vector3.Lerp(gun.position, onKeyPos, Time.deltaTime * lerpSpeed);
     }
     else {
         gun.position = Vector3.Lerp(gun.position, origPos, Time.deltaTime * lerpSpeed);
     }
 }
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
1

Answer by UDN_b5c3e1d7-2c45-4d88-8c12-61a9c2e55780 · Aug 11, 2017 at 06:49 AM

You could try to modify the gun position over time.

 public float lerpSpeed = 1.5f;
 public Transform gun;
 public Vector3 onKeyPos;
 
 Vector3 origPos;
 
 void Start() {
     if (gun == null) {
         Debug.LogWarning('Gun transform is null, the component has been disabled.');
         enabled = false;
         return;
     }
     origPos = gun.transform.position;
 }
 
 void Update() {
     if (Input.GetKey(KeyCode.RightShift)) { //Move the gun towards the onKeyPos
         gun.position = Vector3.Lerp(gun.position, onKeyPos, Time.deltaTime * lerpSpeed);
     }
     else {
         gun.position = Vector3.Lerp(gun.position, origPos, Time.deltaTime * lerpSpeed);
     }
 }
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 · Aug 11, 2017 at 12:52 PM 0
Share
I didnot under stand what are you saying you`re saying "You could try to modify the gun position over time." I didn`t understand, yes try to see below the comment of @bobisgod234 thanks :)
avatar image
0

Answer by · Aug 10, 2017 at 03:46 AM

Sorry, It didnot work for me I put the value of origPos and OnKeyPos and add gun to Gun`s transform after that when play game and press shift key the gun goes far away from camera and as I release key it comes just behind the camera ! :/ Is there any solution for problem ! :) And Yes I add these script in WeaponHolder which is empty Gameobject and contains one more script of sway !

Comment
Add comment · Show 2 · 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 bobisgod234 · Aug 10, 2017 at 06:18 AM 0
Share

Try changing transform.position to transform.localPosition, and gun.position to gun.localPosition. This way the gun will move relative to its parent.

avatar image bobisgod234 · Aug 10, 2017 at 12:41 PM 0
Share

Can you give me script as you are saying I tried didnot working :( Help me!

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

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

gameobject position lerp 0 Answers

How do I get the world coordinates of the top left position of the camera? 3 Answers

How can I make a pong ball move and collide with the paddles and walls without using rigibodies, physics materials or colliders? 1 Answer

Relative world positioning with constraints 0 Answers

How do I make obj1.transform.position.y = obj2.transform.rotation.y 2 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