- Home /
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?
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);
}
}
}
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?
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?
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

Follow this Question
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