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 MC HALO · Dec 18, 2011 at 11:34 PM · recoil

Recoil script help

Hey guys how you doing :) I was wondering if you could help me out well basically i have created a simple recoil script which makes the camera go up and making it look like recoil. Now the only problem is that it goes up when i press or hold the F key but it wont come back down how can i solve this here is my script :

 var playerCamera : GameObject;
 
 
 
 function Update()
 
 {
 
 if(Input.GetKey(KeyCode.F)){
 
 Recoil();
 
 }
 
 }
 
 
 
 function Recoil()
 
 {
 
 playerCamera.transform.localEulerAngles -= Vector3.right;
 
 }


Thank you in advance MCHALO THANK YOU :)

Hey dude i have found a different way but its getting there. Basically at the moment it can go up and down but the problem is when i start the game i have already picked the set the original values of the position it starts of in. And if i shoot it will go up then the cooler kicks in and then it comes back down. Now if when it is going down how do i check if it has passed it original position on the X. lets say x was 1 and when its going down how do i check if its less then 1 so -1 if it set it back to 1. Now i have tried

  if(mainCameraPos.transform.localPosition <1){


then do something but that gives me a new problem

here is my script

 var mainCameraObject: GameObject;
 
 var timerStart : float;
 
 var Cooler : float ;
 
 var Speed: float;
 
 var OriginalPos : Vector3;
 
 var inputTimer : float;
 
 
 
 function Start (){
 
 
 
  OriginalPos = transform.localPosition;
 
 
 
 }
 
 
 
 function Update()
 
 {
 
 
 
 if( timerStart > 0){
 
 
 
   timerStart -= Time.deltaTime;
 
   mainCameraObject.transform.localEulerAngles += OriginalPos ;
 
   
 
 
 
 }
 
 
 
 
 
 if( timerStart < 0){
 
 
 
    timerStart = 0;
 
 
 
 }
 
 
 
 if(Input.GetKey(KeyCode.F)){
 
 
 
   Recoil();
 
   
 
   timerStart = Cooler;
 
   
 
 
 
 }
 
 
 
 }
 
 
 
 
 
 
 
 function Recoil()
 
 
 
 {
 
 
 
  mainCameraObject.transform.localEulerAngles -= Vector3.right * Speed;
 
 
 
 }
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 syclamoth · Dec 19, 2011 at 01:05 AM

Well, you're doing this in a too-simple way. As it is, it only has functionality for moving the camera back- none for moving forward again. Try putting this stuff into a Coroutine-

 function Recoil()
 {
     recoiling = true;
     var currentTime : float = 0;
     var originalRotation : Quaternion = transform.localRotation;
     while (currentTime < recoilTime)
     {
         transform.localRotation = Quaternion.Slerp(originalRotation, recoilRotation, currentTime / recoilTime);
         currentTime += Time.deltaTime;
         yield;
     }
     currentTime = 0;
     while (currentTime < resetTime)
     {
         transform.localRotation = Quaternion.Slerp(recoilRotation, originalRotation, currentTime / resetTime);
         currentTime += Time.deltaTime;
         yield;
     }
     recoiling = false;
 }

Where recoilTime and resetTime are floats which designate how long each stage should take, and recoilRotation is a Quaternion which tells it how far back to lean during the recoil action.

To activate it, use-

 if(!recoiling && Input.GetKey(KeyCode.F))
 {
     Recoil();
 }
Comment
Add comment · Show 11 · 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 MC HALO · Dec 19, 2011 at 01:45 AM 0
Share

It does not work :) none of the time take effect and when i press the F key nothing happens the camera does not shake :). I have declared the variable above needed :)

avatar image syclamoth · Dec 19, 2011 at 01:59 AM 0
Share

Have you set 'recoilRotation' to the correct value? If it is zero, it won't do anything. I would usually set this up by creating a child object of the main transform, rotating it to the correct value, and then use

 recoilRotation = recoilSetter.localRotation;

inside of Start().

avatar image MC HALO · Dec 19, 2011 at 02:01 AM 0
Share

hey :) i tried it but its not what i am looking for this one makes the camera fly up then swings back down i need it to only go up in the Y axis like my old code and then return back to its original position if the timer is increased :)

avatar image syclamoth · Dec 19, 2011 at 02:08 AM 0
Share

Obviously I'm not understanding your question properly. You said

Now the only problem is that it goes up when i press or hold the F key but it wont come back down how can i solve this here is my script :

I've solved that specific problem- it comes back down again directly after rising. Was this not what you were after? Could you please explain your requirements a little more clearly?

avatar image syclamoth · Dec 19, 2011 at 02:08 AM 0
Share

Try tweaking the values- it's possible that you just haven't found a setup that suits you yet.

Show more comments
avatar image
0

Answer by IrshadKhan_ · Mar 28, 2017 at 09:23 AM

Check This

 var recoilMod : Transform;
  var weapon : GameObject;
  var maxRecoil_x : float = -20;
  var recoilSpeed : float = 10;
  var recoil : float = 0.0;
   
  function Update()
  {
      if(Input.GetMouseButton(0))
      {
          //every time you fire a bullet, add to the recoil.. of course you can probably set a max recoil etc..
          recoil+= 0.25;
      }else
      {
      recoil = 0;
      }
   
      recoiling();
  }
   
  function recoiling()
  {
      if(recoil > 0)
      {
          var maxRecoil = Quaternion.Euler (maxRecoil_x, 0, 0);
          // Dampen towards the target rotation
          recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, maxRecoil, Time.deltaTime * recoilSpeed);
          weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
          recoil -= Time.deltaTime;
      }
      else
      {
          recoil = 0;
          var minRecoil = Quaternion.Euler (0, 0, 0);
          // Dampen towards the target rotation
          recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, minRecoil,Time.deltaTime * recoilSpeed / 2);
          weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
      }
  }
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

6 People are following this question.

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

Related Questions

How do I make an enemy recoil when hit? 1 Answer

How to make a camera recoil system 0 Answers

Help with Recoil for guns 3 Answers

[Solved]Top-down shooter camera recoil effect 1 Answer

Vector3.Lerp not working unless interpolant t is 1. 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