I currently have three transforms serving as “Lanes” for a character cube. I want the cube to snap to each transform or lane but smoothly, if i use time.deltaTime then it moves smoothly however i’m using a key press so the cube stops in between lanes rather then smoothly finishing the snap. Here is the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playermovement : MonoBehaviour {
public Transform[] Lanes;
public GameObject Empty;
public GameObject Bounce;
public bool iskeyactive;
public bool isSpace;
// Use this for initialization
void Start () {
iskeyactive = false;
isSpace = false;
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.S))
{
iskeyactive = true;
if(isSpace == false)
transform.position = Vector3.MoveTowards(transform.position, Lanes[0].position, 1);
}
if (Input.GetKeyUp(KeyCode.S))
{
iskeyactive = false;
}
if (Input.GetKey(KeyCode.D))
{
iskeyactive = true;
if(isSpace == false)
transform.position = Vector3.MoveTowards(transform.position, Lanes[1].position, 1);
}
if (Input.GetKeyUp(KeyCode.D))
{
iskeyactive = false;
}
if (Input.GetKey(KeyCode.A))
{
iskeyactive = true;
if(isSpace == false)
transform.position = Vector3.MoveTowards(transform.position, Lanes[2].position, 1);
}
if (Input.GetKeyUp(KeyCode.A))
{
iskeyactive = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
if(iskeyactive == false)
{
isSpace = true;
Bounce.transform.localScale = (new Vector3(1, 0.5f, 1));
Empty.transform.localPosition -= (new Vector3(0, 0.2f));
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
if(iskeyactive == false && isSpace == true)
{
isSpace = false;
Bounce.transform.localScale = (new Vector3(1, 1f, 1));
Empty.transform.localPosition += (new Vector3(0, 0.2f));
}
if (iskeyactive == true && isSpace == true)
{
isSpace = false;
Bounce.transform.localScale = (new Vector3(1, 1f, 1));
Empty.transform.localPosition += (new Vector3(0, 0.2f));
}
}
}
}