• 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 /
  • Help Room /
avatar image
1
Question by dunkel-san · Jul 16, 2021 at 08:50 AM · animationmovement scriptlinear

Lerp Animation Problem

Dear Forum,

I am an experienced 3D animator but only a C# beginner and I have a (probably) very simple problem w$$anonymous$$ch I can´t solve on my own.

In a nutshell:

I have 4 objects (players) and three buttons placed between them. On clicking the buttons I want the objects left and right of the button to switch their positions. Due to the fact that the objects will be passed from button to button I need to check their positions and then decide wether to move them or not. The buttons call the SwitchPos() on click and the script is placed on a parent empty game object and checks the objects by their tag "asset3_objects". Currently I only have one if clause running checking if an object has transform.x equals 4 and needs to be moved to 7

I use the following script:

 using UnityEngine;
 using System.Collections;
 
 public class asset2_master_v2 : MonoBehaviour
 {
     public GameObject[] players;
     public void SwitchPos()
     {
         players = GameObject.FindGameObjectsWithTag("asset3_objects");
         for (int i = 0; i < players.Length; i++)
         {
            // Debug.Log("Player Number " + i + " is named " + players[i].transform.position.x);
             if(players[i].transform.position.x==4)
             {
                 Debug.Log("Object Number" + i + "fits");
                 private float timeElapsed;
                 private float lerpDuration = 30f;
                 private float startValue = 4;
                 private float endValue = 7;
                 private float valueToLerp = players[i].transform.position.x;
                  valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                  timeElapsed += Time.deltaTime;
                 }
             }
         }
     }

The Debug Log tells me that the object fits the position. Now when I add the Lerp components t$$anonymous$$ngs start to go sideways and I get the error codes:

 Assets\scripts\asset3\asset2_master_v2.cs(28,15): error CS1519: Invalid token '.' in class, struct, or interface member declaration
 
 Assets\scripts\asset3\asset2_master_v2.cs(28,36): error CS1519: Invalid token ';' in class, struct, or interface member declaration

Where do I go wrong? I am probably pretty close but I am still learning and do not have the necessary knowledge to solve t$$anonymous$$s on my own. I have read a bit on the Lerp function but the tutorials and documentation cover topics w$$anonymous$$ch are a lot more complex than t$$anonymous$$s.

Thank you and best regards

Dunkel-san

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 andrew-lukasik · Jul 16, 2021 at 09:06 AM 0
Share

1 Reply

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

Answer by dunkel-san · Jul 16, 2021 at 10:18 AM

Hey there, I removed the private keywords and that took care of the CS1519 errors, thank you very much. It still is not working however and now I get a new compilation error w$$anonymous$$ch reads

 Assets\scripts\asset3\asset2_master_v2.cs(28,18): error CS0165: Use of unassigned local variable 'timeElapsed'

I then commented those parts out of the script (because the float did not have a value) and now it reads and still does not work. The debug message runs fine, but the Lerp animation is not working

 using UnityEngine;
 using System.Collections;
 
 public class asset2_master_v2 : MonoBehaviour
 {
     public GameObject[] players;
 
 
     public void SwitchPos()
     {
         players = GameObject.FindGameObjectsWithTag("asset3_objects");
 
         for (int i = 0; i < players.Length; i++)
         {
             if (players[i].transform.position.x == 4)
             {
                 Debug.Log("Object Number" + i + "fits");
 
                 //float timeElapsed;
                 float lerpDuration = 3;
 
                 float startValue = 4;
                 float endValue = 10;
                 float valueToLerp = players[i].transform.position.x;
 
 
                         valueToLerp = Mathf.Lerp(startValue, endValue, lerpDuration);
                     //    timeElapsed += Time.deltaTime;
                 
 
             }
         }
     }
 }




Comment
Add comment · Show 14 · 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 rage_co · Jul 16, 2021 at 10:25 AM 0
Share
avatar image dunkel-san rage_co · Jul 16, 2021 at 11:22 AM 0
Share
avatar image rage_co dunkel-san · Jul 17, 2021 at 01:02 PM 0
Share
Show more comments
Show more comments
avatar image dunkel-san · Jul 19, 2021 at 10:01 AM 1
Share
avatar image rage_co dunkel-san · Jul 19, 2021 at 01:24 PM 1
Share
avatar image dunkel-san rage_co · Jul 19, 2021 at 01:44 PM 1
Share
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.

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

304 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 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 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 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

Sync Anima2D Animations with Player Movement 0 Answers

Problems with Movement on rigged 2d animation., 0 Answers

Character Animator controller/movement 0 Answers

How to make enemy detect a character in his certain range? 1 Answer

Why is Nothing Moving? 0 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