- Home /
How to make a mouse click selection for in game use
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.
Answer by New_Game_Ideas · Sep 19, 2021 at 04:46 PM
I don't think there is a proper way to do it on update. I was just working on it and find a solution. Baiscally I started to increase a number (it is ClickCounter in the code) over-time in FixedUpdate() and when OnMouseDown() I started it from zero again. If the OnMouseUp() is called before the number reaches ten it is assumed a click Note: Do not forget to define ClickCounter(int) or any variable you want to use for the same function, do not forget to define Selected (bool) variable The code:`
region DragDrop
private void FixedUpdate()
{
ClickCounter++;
}
private void OnMouseDown()
{
ClickCounter = 0;
if(IsSelected == true)
{
distance = Vector3.Distance(gameObject.transform.position, Camera.main.transform.position);
}
}
private void OnMouseUp()
{
if(ClickCounter <= 10)
if(gameObject.tag == "Dost birlik")
{
IsSelected = !IsSelected;
}
}
private void OnMouseDrag()
{
if(IsSelected == true)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 rayPoint = ray.GetPoint(distance);
transform.position = new Vector3(rayPoint.x,transform.position.y,rayPoint.z);
}
}
#endregion
` And this hall code requires a click to work properly.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
simple ball rolling mechanic 0 Answers
TextField/User Controls Conflicts 2 Answers
Select something using mouseclick 2 Answers
Character Movement Issues 0 Answers