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 chrisall76 · Sep 14, 2012 at 01:34 AM · lerpgunsight

Lerping doesn't return to the same position?

As the title says, when I lerp my gun to a sight position and back, it goes to a different position than it's suppose to. I can't figure out how/why this happens. Any help? Here's my script:

 public var DefaultPos : Vector3;
 var SightPos          : Vector3;
 var SightBool: boolean = false;
 var Speed    : float = 1.00;
 var AimPosX  : float = 0.00;
 var AimPosY  : float = 0.00;
 var Sight    : GameObject;
 var Gun      : GameObject;
 
 function Start(){
     DefaultPos = transform.localPosition;
     SightPos   = Sight.transform.localPosition;
 }
 
 function Update(){
   if (Input.GetButtonDown("Fire2")){
       Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, SightPos, Time.deltaTime * Speed);
   }
   
   if (Input.GetButtonUp("Fire2")){
       Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, DefaultPos, Time.deltaTime * 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

1 Reply

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

Answer by Codetastic · Sep 14, 2012 at 08:54 AM

Well there are actually several problems I see here:

 //There is no boolean to check if we want to scope in or not (more on that later)
 public var DefaultPos : Vector3;
 var SightPos          : Vector3;
 var SightBool: boolean = false;
 var Speed    : float = 1.00;
 var AimPosX  : float = 0.00;
 var AimPosY  : float = 0.00;
 var Sight    : GameObject;
 var Gun      : GameObject;
 
 function Start(){
     //This should be the guns start position not the transforms.  Unless the transform is the gun of course
     DefaultPos = transform.localPosition;
     SightPos   = Sight.transform.localPosition;
 }
 
 function Update(){
     //This is only called when the button is pushed down so it only runs the lerp for one frame what would be better is setting a boolean to true/false and lerping over time
     if (Input.GetButtonDown("Fire2")){
       Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, SightPos, Time.deltaTime * Speed);
   }
     //same as above
     if (Input.GetButtonUp("Fire2")){
       Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, DefaultPos, Time.deltaTime * Speed); 
   }
 
 }



Here is a better way:

 var defaultPos : Vector3;
 var sightPos : Vector3;
 //a boolean to know if we are scoping in or out
 var scopeIntoSights : boolean = false;
 var speed : float = 1.0;
 var aimPosX : float = 0.0;
 var aimPosY : float = 0.0;
 var sight : Transform;
 var gun : Transform;
 //the percentage we are scoped in
 var scopedInPercent : float = 0.0;
 
 function Start(){
     DefaultPos = gun.localPosition;
     sightPos   = sight.localPosition;
 }
 
 function Update(){
     //Check if Fire2 was pushed down or up and set boolean accordingly
     if (Input.GetButtonDown("Fire2")) scopeIntoSights = true;
     else if (Input.GetButtonUp("Fire2")) scopeIntoSights = false;
     
     //Check if we should scope in or out, and if we have already.
     //If not done with the transition add or take away accordingly
     if(scopeIntoSights == true && !Mathf.Approximately(scopedInPercent,1.0)) scopedInPercent += Time.deltaTime * speed;
     else if(scopeIntoSights == false && !Mathf.Approximately(scopedInPercent,0.0)) scopedInPercent -= Time.deltaTime * speed;
     
     //Lerp takes a value between 0 and 1 so clamp to make sure it stays inside 0 and 1
     scopedInPercent = Mathf.Clamp(scopedInPercent,0.0,1.0);
     
     //apply position based on percent, default position and sight position
     gun.localPosition = Vector3.Lerp(defaultPos, sightPos, scopedInPercent);
 }

Hope that works out for you.

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 chrisall76 · Sep 16, 2012 at 09:54 PM 0
Share

This works perfectly, thanks :)

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

10 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

Related Questions

Changing camera position to shoot through scope on gun 1 Answer

How would you implement ADS for Custom Scopes? 1 Answer

Help with Lerp 0 Answers

Why is .Lerp so bumpy? 3 Answers

Quaternion.Lerp messing up after being deactived and then activated again 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