• 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
Question by tayyab43 · Oct 25, 2013 at 04:19 PM · transformlerpaimdownsights

I am trying to make a script that lerps when a button is pressed then lerps back but its not working?

I am trying to make a script that lerps when a button is pressed then lerps back but its not working, heres what i have so far BTW its in c#: using UnityEngine; using System.Collections;

public class TheProperAimDownSight : MonoBehaviour { public float speed = 5.0f; public GameObject gun; Vector3 positionA = new Vector3 (0.3946627f, 0.5631797f, 0.7112392f); Vector3 positionB = new Vector3(0.009421766f, -0.1324487f, 0.5510964f);

 void Awake ()
 {
 
     
 }
 

void Update () { if(Input.GetMouseButton(1)) { transform.position = Vector3.Lerp(positionA, positionB, Time.deltaTime);

    }
 
    else
    {
      transform.position = Vector3.Lerp(positionB, positionA, Time.deltaTime);
      
    }
   
 } 

}

Comment

People who like this

0 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 sethuraj · Oct 25, 2013 at 04:41 PM 0
Share

Dont use deltaTime for lerptime.Lerptime ranges from 0 to 1.

use something like this

 LerpTime+=Time.deltatime*MoveSpeed;

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Cence99 · Oct 25, 2013 at 04:52 PM

It will only lerp as long as you hold the right mousebutton down. You maybe want to do it this way:

 bool lerp;
 
 void Update ()
 {
     if(Input.GetMouseButtonDown(0))
     {
         lerp = true;
     }
     if(Input.GetMouseButtonUp(0))
     {
         lerp = false;
     }
 
     if(lerp)
         transform.position = Vector3.Lerp(positionA,          positionB, Time.deltaTime);
     else
         transform.position = Vector3.Lerp(positionB, positionA, Time.deltaTime);
 }

With this code it will lerp to positionB if we clicked LMB once, if we release LMB it will lerp back to positionA.

Hope that helps in some way.

Comment
MetalOxide
_invalidusername

People who like this

0 Show 2 · 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 tayyab43 · Oct 25, 2013 at 05:08 PM 0
Share

One problem, i want it in relation to the camera, how would i do this?

avatar image sethuraj · Oct 25, 2013 at 05:10 PM 1
Share

This wont lerp it will just stick to position A or position B.Try it for yourself. :-)

avatar image

Answer by sethuraj · Oct 25, 2013 at 05:08 PM

First of all please format your code the next time you paste it.

Here is the solution.

 using UnityEngine;
 using System.Collections;
  
 public class TheProperAimDownSight : MonoBehaviour 
 {   
     //Speed at w$$anonymous$$ch lerp should occur
     public float speed = 1.0f;
  
     //A and B vectors to w$$anonymous$$ch lerp should occur
     Vector3 positionA =new Vector3(0,0,0);
     Vector3 positionB=new Vector3(0,10,0);
  
     //Lerp Direction
     bool LerpedUp=false;
     //Lerp time value
     float LerpTime=1.0f;
  
     void Update()   
     { 
        //If HoldOn Right Mousebutton,Move from Point A to B  
        if(Input.GetMouseButton(1)) 
        {
          if(!LerpedUp)
          {
           //Reset LerpTime
           LerpTime=0.0f;
           //State Lerping Up(A to B)
           LerpedUp=true;
          }         
          else if(LerpTime<1.0f)
          {         
           transform.position = Vector3.Lerp(positionA , positionB,LerpTime);          
           LerpTime+=Time.deltaTime*speed;
          }
  
        }  
        else//If released Right Mousebutton,Move from Point B to A
        {
          if(LerpedUp)
          {
           //Reset LerpTime
           LerpTime=0.0f;
           //State Lerping Down(B to A)
           LerpedUp=false;
          }         
          else if(LerpTime<1.0f)
          {
             transform.position = Vector3.Lerp(positionB, positionA , LerpTime);
           LerpTime+=Time.deltaTime*speed;
          }
        } 
     } 
 }
Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

I want to lerp a gun from positionA to positionB in relation to the main camera 1 Answer

Camera Lerp from A to B, and back 2 Answers

How to use Vector3.Lerp without slow-down 3 Answers

bounce player back to position while jumping 0 Answers

Smoothly Rotate Object Towards Target Object 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