Hello! Im trying to make my first game, and i need some help on how to make my object move from A to B when i press “a”, and from B to A when i press “a” again. I have found a script that moves my object from A to B, but i can’t figure out how to make it go from B to A when i press “a” again.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class lerp : MonoBehaviour {
public GameObject player;
private Vector3 pos1;
private Vector2 pos2;
private float distance = 4.7f;
private float time = 0.05f;
private float currentTime = 0;
private bool keyHit = false;
// Use this for initialization
void Start () {
pos1 = player.transform.position;
pos2 = player.transform.position + Vector3.right*distance;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("a")) { keyHit = true; }
if (keyHit == true)
{
currentTime += Time.deltaTime;
if (currentTime >= time) { currentTime = time;
}
float Perc = currentTime / time;
player.transform.position = Vector3.Lerp(pos1, pos2, Perc);
}
}
}