I am fairly new to unity although I have some prior experience coding in C#. I am trying to make a basic RTS game and have been working on getting units to move properly and was looking for some help.
On my unit I have the following script`using UnityEngine;
using System.Collections;
public class UnitSelect : MonoBehaviour {
// Use this for initialization
void Start ()
{
}
public int isHit;
public int isSelected;
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if(Physics.Raycast(ray,out hit))
{
isHit = 0;
isSelected = 1;
//Destroy(GameObject.Find(hit.collider.gameObject.name));
}
}
}
}
`
and then on my camera I have this script
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
public GameObject unit;
public GameObject unit1;
public float unitSpeed = 0.00001f;
private UnitSelect unitSelect;
void Awake()
{
unitSelect = unit.GetComponent<UnitSelect> ();
unit1 = GameObject.Find("SphereUnit");
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (1))
{
Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit2 = new RaycastHit();
if (Physics.Raycast(ray2, out hit2));
{
Debug.Log("Right click works");
if (unitSelect.isSelected == 1)
{
Debug.Log("Working");
unit1.renderer.material.color = Color.red;
unit1.transform.Translate( hit2.point * unitSpeed * Time.deltaTime);
}
}
}
}
}
The problem I am having is that the unit will move only a little way to the destination of the mouse click and then if you click somewhere else it will move in a seemingly random direction. I am also unsure on how to get this to work for multiple units so any suggestions for that would be welcome. I have Googled this issue but all the answers I found where working in Jacascript.
Thanks