Hello I am trying to use the space bar to move from one location to another. When I press Space, my object moves to one location and stays there. I want the object to move to location A when I press spacebar, and then move to location B once i press spacebar again.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class lerpFinal : MonoBehaviour
{
//This is a new variable to store a Vector3 Position
private Vector3 newPosition;
//Here we set the current position of the object using transform.position
private void Awake()
{
newPosition = transform.position;
}
//Here we are running positionChanging function in each framej
void Update()
{
PositionChanging();
}
void PositionChanging()
{
//Here is Where the positions are set
Vector3 positionA = new Vector3(-5, 3, 0);
Vector3 positionB = new Vector3(5, 3, 0);
//Inputing the following keys will allow us to Change the Position
if (Input.GetKeyDown(KeyCode.Space))
newPosition = positionA;
if (Input.GetKeyDown(KeyCode.Space))
newPosition = positionB;
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime);
}
}