Alright so i have been developing a level builder game for a little while now and my biggest problem to date is getting the mouse click selection system working. What I need it to do is simple enough. When the left mouse button is clicked and the code recognizes that it has hit an object it needs to highlight or somehow note that the object is selected and then pass a variable to the object so that the player can use the keyboard controls to move and rotate the object, and only the object selected. Bellow is the code i have thus far:
var clickedGmObj:GameObject = null;
function GetClickedObject()
{
var ray:Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit:RaycastHit;
if(Physics.Raycast(ray, Mathf.Infinity))
{
return hit.transform.gameObject;
}
else
{
return null;
}
}
function Update () {
if(Input.GetMouseButtonDown(0))
{
clickedGmObj = GetClickedObject();
if(clickedGmObj != null)
Debug.Log(clickedGmObj.name);
}
}
this is the code used for the mouse selection, next is the code applied to the objects so they can be moved:
var distance = 10;
function Start () {
}
function Update () {
if(Input.GetKey("w"))
{
transform.Translate(Vector3.forward * distance * Time.deltaTime);
}
if(Input.GetKey("s"))
{
transform.Translate(Vector3.back * distance * Time.deltaTime);
}
if(Input.GetKey("a"))
{
transform.Translate(Vector3.left * distance * Time.deltaTime);
}
if(Input.GetKey("d"))
{
transform.Translate(Vector3.right * distance * Time.deltaTime);
}
if(Input.GetKey("r"))
{
transform.Translate(Vector3.up * distance * Time.deltaTime);
}
if(Input.GetKey("f"))
{
transform.Translate(Vector3.down * distance * Time.deltaTime);
}
if(Input.GetKey("e"))
{
transform.Rotate(Vector3(0,45,0) * Time.deltaTime);
}
if(Input.GetKey("q"))
{
transform.Rotate(Vector3(0,-45,0) * Time.deltaTime);
}
}
its all javascript, any and all help would be much appreciated. im basically a novice at this. ive only gotten this far with alot of help from a few friends at school.