I’ve been working on a basic chess game, and as part of the system I want the pieces to move around as if they’re being pushed, so I thought Vector3.Lerp would be perfect. I’ve used it before with few issues, but I thought I’d go on the safe side and get a script from here, which I’ve done before. However, instead of the piece moving smoothly from 'origin to ‘target’, it instantly teleport from one to the other. I get an error
‘transform.position assign attempt for ‘King’ is not valid. Input position is { NaN, NaN, NaN }.’ in line ‘Tiles:Move() (at Assets/Scripts/Tiles.cs:40)’, which I’m pretty sure is coming from ‘distCovered’ being 0 and messing the whole script up, but I have no idea how it coul be becoming 0, as it isn’t 0 at the end on the ‘On MouseDown’ function. Here’s my script:
public class Tiles : MonoBehaviour {
public float journeyLength;
public float startTime;
static public bool moving = false;
public float speed = 0.1f;
private Color changeColor = new Color(0, 0.5f, 1, 0);
void Update ()
{
if (moving == true)
{
Move();
}
}
void OnMouseDown()
{
if (gameObject.GetComponent<Renderer>().material.color == changeColor)
{
Manager.target = this.gameObject.transform.position;
Manager.origin = Manager.selected;
Vector3 v3 = new Vector3(Manager.target.x, Manager.origin.transform.position.y, Manager.target.z);
Manager.target = v3;
startTime = Time.time;
journeyLength = Vector3.Distance(Manager.origin.transform.position, Manager.target);
moving = true;
}
}
void Move ()
{
float distCovered = (Time.time - startTime) * speed;
Debug.Log(journeyLength);
Debug.Log(distCovered);
float fracJourney = distCovered / journeyLength;
Manager.selected.transform.position = Vector3.Lerp(Manager.origin.transform.position, Manager.target, fracJourney);
if(Manager.selected.transform.position.x == Manager.target.x || Manager.selected.transform.position.z == Manager.target.z)
{
moving = false;
}
}
}
All I did was paste in the script from the above link and change the variables to be what I wanted.
Any help is greatly appeciated, sorry if this is a really nooby question, I couldn’t find an anwser anywhere :(.