Hi everyone,
I’m a beginner and I’m sure I’m missing something obvious, but my Lerp code does not work the way I want it to. Basically I’m trying to make a little on-screen keyboard, and I’d like to set it up so that when the player hits a key, the key on the screen moves down and then back up. Any advice would be appreciated! I have searched a lot but apparently in the wrong places.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveKeysDown : MonoBehaviour
{
public Transform keyDown;
public Transform keyUp;
private Transform startPos;
private Transform endPos;
void Start()
{
startPos = keyUp;
endPos = keyDown;
}
void Update()
{
if (Input.GetKeyDown("space"))
{
Debug.Log("Space key was pressed");
transform.position = Vector3.MoveTowards(startPos.position, endPos.position, Time.deltaTime);
}
if (Input.GetKeyUp("space"))
{
Debug.Log("Space key was released");
transform.position = Vector3.MoveTowards(endPos.position, startPos.position, Time.deltaTime);
}
}
}