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 Waterdropz · Jun 18, 2015 at 06:23 AM · c#vector3translateempty

How to translate an empty?

I've created a weapon recoil script where when you hold down left click, a variable number gradually increases by 2.5 per second. While this number increases i want an empty to translate upwards by the variable number. Once you release left click the variable number slowly decreases by 3 per second, and the empty moves back down gradually. The max height is 4, and the minimum is 0. I dont want the empty object to snap from position a to b, i want it to slowly go up by float values.

  public float RecoilPerSecond = 1.25f;
  public float RecoilRecovery = 0.30f;
  public int MaxRecoil = 4;
  public float MinRecoil = 0.0f;
  public bool IsFiring = false;
  public float CurrentRecoil = 1f;

 void Update () {
     //When holding left click call recoil multiplier and cancel recoil reducer
     if(Input.GetMouseButtonDown (0)){
         IsFiring = true;
         if(IsFiring == true){
             recoilRepeater();
             CancelInvoke("minRecoil");
         }
     }

     //When releasing left click call recoil reducer script and cancel recoil multiplier
     if(Input.GetMouseButtonUp(0)){
         IsFiring = false;
         CancelInvoke("recoilMultiplier");
         minCheck();
     }
 }

 //Function to increase recoil every 0.1 seconds by 0.75
 void recoilMultiplier (){
     RecoilPerSecond *= 1;
     CurrentRecoil += RecoilPerSecond;
     if (CurrentRecoil >= 4) {
         CurrentRecoil = 4;
     }
 }

 //Constantly repeating recoil multiplier
 void recoilRepeater (){
     InvokeRepeating ("recoilMultiplier", 1, 0.10f);
 }

 //Function to reduce recoil every 0.1 seconds by 0.3
 void minRecoil (){
     CurrentRecoil -= RecoilRecovery;
     if (CurrentRecoil <= MinRecoil) {
         CurrentRecoil = MinRecoil;
     }
 }

 //Constantly repeating recoil reducer
 void minCheck (){
     InvokeRepeating ("minRecoil",1,0.10f);
 }

}

Comment
Add comment · Show 1
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 Owen-Reynolds · Jun 18, 2015 at 03:22 PM 0
Share

Just looking at the title of the Q: empties move the same way as any other gameObject. Translate them the same way as anything else.

It might help to make a a marker for it. $$anonymous$$aybe a tiny, bright colliderless ball, childed at 000 to the empty (so it's always exactly where the empty is.)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by HammerCar · Jun 18, 2015 at 07:08 PM

I think you could just use Vector3.lerp

http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

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 rhbrr5hrfgdfgw · Jun 18, 2015 at 03:55 PM

Something like:

         public GameObject empty;//put in editor
         public float MaxRecoil = 4.0f;
         public float sumY = MaxRecoil + empty.transform.localPosition.y;
         public float minRecoil = ?;//Find the value you want in theeditor
         public float increaseValue = 2.5;
         public float decreaseValue = 3;
 
 void Update(){
         if(IsFiring && empty.transform.localPosition.y <= sumY){
         
         empty.transform.localPosition.y += increaseValue *Time.deltaTime;
         
         }else if(!isFiring && empty.transform.localPosition.y > minRecoil){
         
         empty.transform.localPosition.y -= decreaseValue *Time.deltaTime;
         
         }
     }
 

Its untested code

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3.Lerp moves in the wrong direction 1 Answer

How to get this object to move 2 Answers

Multiple Cars not working 1 Answer

Move GameObject along a line according to the players look direction 3 Answers

Distribute terrain in zones 3 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