So I want to create a turn based system similar to Fallout 1 but in 3D. I’ve started with the movement script and I kept it as simple as possible. It creates a plane that the square follows based on mouseposition. On mouse click the gameobject moves to the where the square was last clicked and cannot be clicked again till the gameobject has finished moving. The script seems pretty solid. Does anyone have any suggestions for improvements or something I missed that would simplify some of the processes?
using UnityEngine;
using System.Collections;
public class SquareMovement : MonoBehaviour {
public float speed = 10;
private Vector3 targetPos;
public bool isMoving;
public GameObject square;
public Vector3 squarePos;
void Update ()
{
if(squarePos == Vector3.zero){
if(Input.GetMouseButtonDown(0)){
squarePos = square.transform.position;
}
}
Plane plane = new Plane(Vector3.up, square.transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float point = 0f;
if(plane.Raycast (ray, out point)){
targetPos = new Vector3(Mathf.Round(ray.GetPoint(point).x),Mathf.Round(ray.GetPoint(point).y),Mathf.Round(ray.GetPoint(point).z));
square.transform.position = Vector3.MoveTowards(square.transform.position, targetPos, speed);
if(squarePos != Vector3.zero){
transform.position = Vector3.MoveTowards(transform.position, squarePos, speed * Time.deltaTime);
if(transform.position == squarePos){
squarePos = Vector3.zero;
}
}
}
}
}