• 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 Claymore · Sep 17, 2012 at 05:07 PM · fpsgunrecoil

Camera Recoil,Please help me.

Hi,i want camera recoil (separate script) which you add to camera and when you press shoot button (left mouse button) camera shake off and gets back in position,please guys help me.

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
2
Best Answer

Answer by Griffo · Sep 18, 2012 at 02:30 PM

This will do it.

 #pragma strict
 
 var recoilSpeed : float = 0.01;    // Speed to move camera
 function Start () {
 
 }
 
 function Update () {
 
 if (Input.GetKeyDown ("left ctrl")){
     recoilBack();
     }
 
 if (Input.GetKeyUp ("left ctrl")){
     recoilForward();
     }
 
 }
 
 // Move current weapon to zoomed in position smoothly over time
 function MoveToPosition(newPosition : Vector3, time : float){
     var elapsedTime : float = 0;
     var startingPos = transform.position;
     
     while (elapsedTime < time){
         transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
         elapsedTime += Time.deltaTime;
         yield;
     }
 }
 
 function recoilBack(){
     
 // Start coroutine to move the camera up smoothly over time
         var zoomOutOffset = Vector3(0, 0, 0.5);
         var zoomOutWorldPosition = transform.TransformDirection( zoomOutOffset );
 // Move the camera smoothly 
         StartCoroutine(MoveToPosition(transform.position + zoomOutWorldPosition, recoilSpeed));             
 }
     
 function recoilForward(){
     
 // Start coroutine to move the camera down smoothly over time
         var zoomInOffset = Vector3(0, 0, -0.5);
         var zoomInWorldPosition = transform.TransformDirection( zoomInOffset );
 // Move the camera smoothly 
         StartCoroutine(MoveToPosition(transform.position + zoomInWorldPosition, recoilSpeed));
 }
Comment
Add comment · Show 5 · 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 AlucardJay · Sep 18, 2012 at 02:40 PM 0
Share

Wow, this is very generous of you Griffo. Of course it is great, and future users will gain from absorbing what this script does. I have of course (and if people who recognize my handle will know) supplied scripts to the 'beginner', but with this person I did not feel that they were invested in expanding their knowledge, this being an example : http://answers.unity3d.com/questions/281777/player-destroys-when-he-touches-particle-effect.html

Another indicator is the OPs karma rating. But I stand with my original comment, well done Griffo, I am upvoting this for your generosity (Fattie gets one too) =]

I hope other readers find this useful, and Claymore you should be very thankful.

avatar image Griffo · Sep 18, 2012 at 02:58 PM 0
Share

Hi alucardj,

I totally agree with your comment, maybe I was a bit to quick to answer and never really looked at the users profile, as I usually do, I remember being new here and getting help so like to help if I can as I'm still very new to scripting and I'm sure you have helped me in the past.

I know the community like to see people trying before giving help and I stand by that, I find it had work writing scrips (but enjoyable) and don't like to give them away for no reason and I'm sure others feel the same.

On the other hand its nice to be helped out and we all benefit in the end.

Cheers.

EDIT .. I didn't read the whole page and have just seen you post below, sorry I will be more careful in future.

avatar image Griffo · Sep 18, 2012 at 03:04 PM 1
Share

Fattie,

I know but I thought he could work that bit out for himself .. and It shouldn't take much to workout how to change it to recoil ..

avatar image AlucardJay · Sep 18, 2012 at 03:05 PM 1
Share

True, it is for the greater good, for me the username stood out therefore the question gained 'personality'. I started Unity in January, and I would have struggled alot without having this 'site for a resource.

$$anonymous$$y final advice to Claymore :

http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner

http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html

also : http://answers.unity3d.com/questions/133869/how-to-ask-a-good-question.html

avatar image AlucardJay · Sep 18, 2012 at 03:07 PM 0
Share

ps i thought the same regarding the recoil factor, if the OP was sincere then maybe it would be worth expanding on, any future readers can post a comment to find out.

avatar image
0

Answer by Rahkola · Sep 18, 2012 at 10:43 AM

You could tweak your camera position and rotation with iTween to achieve cool recoil effect.

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 AlucardJay · Sep 18, 2012 at 01:43 PM 0
Share

This is not the place to ask for scripts. Unity Answers and its community is here to help you solve any Unity-related problems you have, not give away free code. If you don't know how to write code, I suggest learning Unity's Java Script (its really easy), but if you don't have time, or whatever, then you can try asking your question on the Unity Forums. I hope you can get an answer, although both these answers have answered the question already.

Also, use google. Do tutorials, and learn how to use Unity.

avatar image AlucardJay · Sep 18, 2012 at 02:35 PM 0
Share

Just a point-of-fact, there is no camera recoil script that comes with Unity.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Need help with bullet/gun scripting 3 Answers

How to script a response to an instantiation? 1 Answer

FPS Shoooting Problem 1 Answer

Problem with First Person Shooter Script 1 Answer

Weapon Possition Move On Sprint 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