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 SwiftLee · Oct 10, 2015 at 04:38 PM · shootinggunrecoil

How To Keep My Weapon With My Camera?

Hello,

I've just run into a problem. My gun does not stay with my camera when it begins to recoil, and after the methods being to run, it "Lerps" back to where it is supposed to. Here is what issue I am experiencing:

alt text

Here are the methods, and I have properly assigned my weapon transform object to my weapon as well.

  IEnumerator MoveToPosition(Vector3 newPosition, float time)
     {
         float elapsedTime = 0;
         Vector3 startingPos = weapon.transform.position;
 
         while (elapsedTime < time)
         {
             weapon.transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
             elapsedTime += Time.deltaTime;
             yield return null;
         }
     }
 
     void recoilBack()
     {
         Vector3 zoomOutOffset = new Vector3(-0.01f, 0, 0);
 
         if (zoomOutOffset.x >= 0.05f)
         {
             zoomOutOffset.x = 0.05f;
         }
 
         var zoomOutWorldPosition = weapon.transform.TransformDirection(zoomOutOffset);
 
         StartCoroutine(MoveToPosition(weapon.transform.position + zoomOutWorldPosition, recoilSpeed));
 
         
     }
 
     void recoilForward()
     {
         Vector3 zoomInOffset = new Vector3(0.01f, 0, 0);
 
 
         if (zoomInOffset.x >= 0.05f)
         {
             zoomInOffset.x = 0.05f;
         }
 
         var zoomInWorldPosition = weapon.transform.TransformDirection(zoomInOffset);
 
         StartCoroutine(MoveToPosition(weapon.transform.position + zoomInWorldPosition, recoilSpeed));
 
     }

c5rd5ey.png (341.8 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tony_T · Oct 10, 2015 at 06:52 PM

Make sure the weapon is child of the camera. Also if you have animated the weapon via Unity it probably uses the global positions so it's most likely it will return to the position your firstly animated. I would recommend animating the weapon in a the software you made the weapon so it is attached to the character and play it no matter where you are in the world.

Comment
Add comment · Show 3 · 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 SwiftLee · Oct 10, 2015 at 10:50 PM 0
Share

Nothing to do with animations, read the code please, everything is already setup properly in the GUI.

avatar image Tony_T SwiftLee · Oct 11, 2015 at 08:55 AM 0
Share

You are not explaining your problem that good. On the title it says "How To $$anonymous$$eep $$anonymous$$y Weapon With $$anonymous$$y Camera?" then you say it has nothing to do with animation and that the GUI is already set up. It's just confusing.

avatar image SwiftLee Tony_T · Oct 11, 2015 at 03:08 PM 0
Share

I think you are misunderstanding. There is no physical animation for the gun, all of the gun movement is done in scripts and is not animated in any other 3D software. It is just a single model with scripts applied to it to have a recoil animation added to it. The methods I gave in my example are causing what you see in the screenshot. The screenshot only occurs when I begin to move while shooting, as the gun doesn't transform its position with the camera. That is the only problem, everything in Unity's interface is already properly set up, from weapon camera to main camera, and to bullet spawn game object's and more. The hierarchy is not the issue, it is the methods supplied. I hope that clears any confusion for you! Thanks!

avatar image
0

Answer by SwiftLee · Oct 11, 2015 at 04:42 AM

No luck yet, finally fixed some of my sounds in the meantime... Any suggestions as to how I can get the gun to continue to recoil and move along with my player/camera? The methods I supplied are called when the IEnumerator Coroutines are run. Thanks for your help guys, please let me know if you have a solution!

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

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

How to attach shooting gun to my eyes???(HTC vive) When I press the button on the controller, it should fire from my eyes. But I can't find the way!! please help me what to do!! Best regards, 1 Answer

Please help me solve the problem with shoot a gun(HTC VIVE) 1 Answer

Gun recoil with mouse look script help 0 Answers

How to make an automatic fire mode for a gun (Javascript) 0 Answers

How to script shooting to trigger shooting sound 1 Answer


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