Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 gamers1432 · Aug 21, 2018 at 08:26 AM · 2dmovementlerping

Move smoothly from left to right side of screen

I feel like I'm missing something here. I'm currently trying to lerp on touch from current position to my destination. For whatever reason it just does not want to work. I'm trying to replicate the movement of Break Liner by Ketchapp.

https://www.youtube.com/watch?v=GmbEHhv0vZU

Another idea I had was to use a rigidbody2d and have the player moving up and then lerping to the position but that does not work either. Posting code below.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class InputManager : MonoBehaviour
 {
     public GameObject player;
     public float speed = 1f;
     // Use this for initialization
     void Start ()
     {
         player.transform.position = new Vector3(-2, 0, 1);
     }
    
     // Update is called once per frame
     void Update ()
     {
         PlayerInput();
         player.GetComponent<Rigidbody2D>().velocity = Vector2.up;
     }
  
     public void PlayerInput()
     {
         if(Input.GetMouseButtonDown(0))
         {
             if(player.transform.position.x == -2)
             {
                 Debug.Log("Pressing");
                 player.transform.position = Vector3.Lerp(transform.position, new Vector3(2, 0, 1), 1);
             }
             else if(player.transform.position.x == 2)
             {
                 Debug.Log("Pressing Two");
                 player.transform.position = Vector3.Lerp(transform.position, new Vector3(-2, 0, 1), 1);
             }
         }
     }
 }
  

To add a little bit more detail: when I change to
player.transform.position = Vector3.Lerp(transform.position, new Vector3(2, 0, 1), Time.deltaTime);
instead of lerping to the new value of 2 on the X, the players nnew position is -43x 20y -4z. What is causing the plays position to be so far off from the new vector I'm giving it?

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
0

Answer by dan_wipf · Aug 21, 2018 at 08:41 AM

have you checked out the docs about vector3.lerp? could be usefull for your problem.


EDIT: I took out the Example of the Vector3.Lerp from UnityDocs, added fixed Code of your Question. Hope it's what you're looking for. changed some lines inside Of the Transform (Y coordinate is always the transform.position.y coordinate or you will stay always grounded and never go up(driven from the Riggidbody2D)), because the Input.MouseButton is a OneTime event and for Lerping you need a Update Function, which you can see below. Further more I added a bool which functions like a PingPong effect and added Speed to your Lerping function so it's a Smooth transition.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
   
 public class InputManager : MonoBehaviour
 {
      public float posOne = 2,posTwo = -2;
      public GameObject player;
      public float speed = 1f;
      public bool m_switch;
      
      // Use this for initialization
      void Start ()
      {
          player.transform.position = new Vector3(posTwo, 0, 1);
      }
     
      // Update is called once per frame
      void Update ()
      {
          PlayerInput();
          player.GetComponent<Rigidbody2D>().velocity = Vector2.up;
      }
   
      public void PlayerInput()
      {
          
          if(Input.GetMouseButtonDown(0))
          {
             m_switch = !m_switch;
          }
               if(m_switch){
                 Debug.Log("Pressing");
                 player.transform.localPosition = Vector3.Lerp(transform.position, new Vector3(posOne, transform.position.y, 1), Time.deltaTime * speed);
             }
             else if(!m_switch){
                 Debug.Log("Pressing Two");
                 player.transform.localPosition = Vector3.Lerp(transform.position, new Vector3(posTwo, transform.position.y, 1), Time.deltaTime * speed);
             }
 
     }
 }

Comment
Add comment · Show 3 · 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 gamers1432 · Aug 21, 2018 at 08:54 AM 0
Share

I have checked that out and didn't know for sure if that would give me the correct curve at the end near the new position. How would I loop t with this being for touch input?

avatar image gamers1432 · Aug 21, 2018 at 09:01 AM 0
Share

To add a little bit more detail: when I change to
player.transform.position = Vector3.Lerp(transform.position, new Vector3(2, 0, 1), Time.deltaTime);
ins$$anonymous$$d of lerping to the new value of 2 on the X, the players nnew position is -43x 20y -4z. What is causing the plays position to be so far off from the new vector I'm giving it?

avatar image dan_wipf gamers1432 · Aug 21, 2018 at 02:01 PM 0
Share

because leaping is between two values by the factor 1. For example:

 $$anonymous$$athf.Lerp(Value1,Value2,t);
 
 Value 1 = 0;
 Value 2 = 10;
 
 t = 0.25f;
 
 

if t is 0.25f the output will be 2.5. if t is 0.1 the output will be 1. if t is 0 the output will be 0. if t is 1 the output will be 10. and so. if t is bigger than 1 the output will show weird results. as well when t is bellow -1. the same happens, thats why you get these strange Coordinates..

hope this helps you understand how lerp function works in Unity.

Adding speed to a Lerp I always use this:

 public speed = 1;
 
 Vector3 Vec1 = new Vector3(0,10,0);
 Vector3 Vec2 = new Vector3(0,0,0);
 
 transform.position = Vector3.Lerp(Vec1,Vec2, Time.deltaTime * speed);
 
 

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

219 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

Related Questions

Physic function doesn't work in different resoultions 0 Answers

How can I make a collision sensitive teleporter for a 2D game #c 1 Answer

Absolute character movement and collision 0 Answers

Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer

How to keep the last good position when interacting with triggers? 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