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 /
avatar image
0
Question by Joeman9832 · Sep 07, 2017 at 12:09 AM · transformtransform.rotationtransform.rotate

transform.rotate

I'm working on recoil for weapons and i decided to use transform.rotate and it works well kinda... i need it to not bounce back to its original position and rather just rotate -5 and then with the next shot rotate another -5. but it bounces back. this is most likely due to me not know the language very well. please help if you can.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class recoil : MonoBehaviour {
     public float fireRate = .16f;
     public float ammo = 20f;
     private float nextFire;
     private WaitForSeconds shotDuration = new WaitForSeconds(0.07f); 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire) {
             nextFire = Time.time + fireRate;
             ammo -= 1;
             transform.Rotate(-5, Time.deltaTime, 0, Space.Self);
             
         }
 
         if(ammo == 0)
         {
             ammo = 20;
         }
 
         if(ammo < 20 && ammo > 0 && Input.GetKeyDown(KeyCode.R))
         {
             ammo = 20;
         }
     }
 }

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by unit_nick · Sep 07, 2017 at 09:42 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class test : MonoBehaviour
 {
     public float fireRate = 0.16f;
     public float ammo = 20f;
 
     // total recoil influence of a fire action
     public float recoilDegrees = 5f;
     // time it will take for gun to reach full recoil
     public float recoilTime = 0.16f;
 
     // recoil variables
     private float prevFire;
     private float prevRecoil;    
     private float totalRecoil; // a movie by Arnold Schwarzenegger :D
 
     private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
 
     // Use this for initialization
     void Start()
     {
         prevFire = prevRecoil = 0;
         totalRecoil = float.MaxValue;
     }
 
     // Update is called once per frame
     void Update()
     {
         // get the burrent time
         float time = Time.time;
 
         // fire if enough time has passed sine last shot
         if (Input.GetKey(KeyCode.Mouse0) && time > prevFire + fireRate)
         {
             prevFire = time;
             prevRecoil = totalRecoil = 0;
             ammo -= 1;
         }
 
         // if still recoiling then apply recoil
         if (totalRecoil < recoilDegrees) {
 
             // get the total recoil for this time
             totalRecoil = recoilDegrees * Mathf.Sin(90f * (1f / recoilTime) * Mathf.Min(time - prevFire, recoilTime) * Mathf.Deg2Rad);
 
             // apply the total recoil less the previous recoil. make it neg to go up
             transform.Rotate((totalRecoil - prevRecoil) * -1f, 0, 0, Space.Self);
 
             // save last recoil for next loop
             prevRecoil = totalRecoil;
         }
 
         if (ammo == 0)
         {
             ammo = 20;
         }
 
         if (ammo < 20 && ammo > 0 && Input.GetKeyDown(KeyCode.R))
         {
             ammo = 20;
         }
     }
 }
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
0

Answer by N00MKRAD · Sep 07, 2017 at 07:58 AM

Uh, just use animation for recoil.

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
0

Answer by tormentoarmagedoom · Sep 07, 2017 at 08:10 AM

Hello @Joeman9832 ,

As Ju_M says, is 100% recomended to use animation. Is more easy than scripting, and you can see the results as you make it. Look some videos to learn if dont know how to do it, but in less than 15 minutes you will know all you need for do it!

:D

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 Joeman9832 · Sep 07, 2017 at 09:54 PM 0
Share

i may have been hard to understand what i want is to have the camera rotate and shake like the gun is pushing you up. i have animations on the gun. ill include an image of what im wanting.alt text

untitled.png (18.2 kB)
avatar image
0

Answer by Joeman9832 · Sep 07, 2017 at 10:27 PM

Thank you so much unit nick

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 Joeman9832 · Sep 07, 2017 at 10:35 PM 0
Share

unit nick i still have the problem of it bouncing back to the original spot im not sure why

avatar image unit_nick Joeman9832 · Sep 08, 2017 at 04:03 AM 0
Share

I would need to see more code. Nothing else in the code provided moves the object.

Also I made the recoil decelerate by gradually slowing because that was my first idea. However thinking about it I might make it linear.

avatar image unit_nick Joeman9832 · Sep 08, 2017 at 04:09 AM 0
Share

Although reading your comment with diagram above this may have been a lucky coincidence. If it is the camera you are rotating then not having it come to a sudden stop might be appropriate?

avatar image Joeman9832 unit_nick · Sep 08, 2017 at 04:36 AM 0
Share

yah i would like to smooth out the motion and also have it continue its path upwards if the player continues to shoot but im not sure how i would do that

Show more comments

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

85 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

Related Questions

Clamping X rotation on camera. 1 Answer

Adding quaternions using a seperate axis 0 Answers

transform.Rotate slows down my moving cube 0 Answers

Issues with copying Y axisrotation of another object 2 Answers

Quaternion.LookRotation() doesn't look down 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