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 3 · Mar 26, 2013 at 04:09 AM · transformtranslaterecoil

Proper Recoil for gun?

So I have a working script for recoil, but the thing is, it jumps to the spot then jumps back, Is there any way to make a smooth transition for recoil? So it gradually goes up, rather than just instantly moving up?

 #pragma strict
 
 function Start(){
 
 }
 var triggerfire : GameObject;
 function Recoil(){
  transform.Rotate(Vector3.down * Time.deltaTime * 1875);
 }
 function Recoilreturn(){
  transform.Rotate(Vector3.up * Time.deltaTime * 1875);
 }
 function Update(){ 
    if(Input.GetMouseButtonDown(0) || (Input.GetMouseButtonDown(1)) || (Input.GetMouseButtonDown(2)) || (Input.GetKeyDown (KeyCode.Space))){
     Invoke("Recoil", .221);
     //triggerfire.animation.Play("fire1");
     Invoke("Recoilreturn", .421);
         }
 }
Comment
Add comment · Show 2
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 robertbu · Mar 26, 2013 at 06:13 AM 0
Share

There's nothing in this script that does recoil. All this script does is shoot the bullet.

avatar image 3 · Mar 26, 2013 at 06:28 AM 0
Share

Wow, sorry wrong script Just changed it, my bad.

Thanks,

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Griffo · Mar 26, 2013 at 06:55 AM

Try using this, it can be used for smoothly zooming a weapon or you can adapt it to suit your means, alter the Vector3 to move the direction you want and play around with the speed, should point you in the right direction -

 #pragma strict
   
 var recoilSpeed : float = 0.01;    // Speed to move Gun/Camera/ up and down
 
 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 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 3 · Mar 26, 2013 at 09:36 PM 0
Share

not, sure, It didn't seem to work

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

11 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

Related Questions

Rigidbody Sink into BoxColider 0 Answers

Moving character on x axis 1 Answer

Dont transform.translate if it will collide with another object 1 Answer

How can I instantiate and move objects smoothly? 1 Answer

C# How to make a gameobject move a max distence at a certain speed 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