- Home /
Question by
Demondarem · Jul 25, 2018 at 12:29 PM ·
c#gameobjectpositionscale
I tried to make a box to select units like you do in windows
It's position and scale should be assigned via bounds , but it's position weirdly kinda like snaps to 0,0,0 position . Scale seems weird too... I don't know how to fix it HELP!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CursorScript : MonoBehaviour {
public GameObject theEffect;
public GameObject theBlock;
public bool selection;
private bool once;
public Transform point1;
public Transform point2;
private GameObject CurrentBlock;
private bool oncee;
private void Start()
{
Cursor.visible = false;
}
Vector3 Center()
{
var bounds = new Bounds();
bounds.Encapsulate(point1.transform.position);
bounds.Encapsulate(point2.transform.position);
return bounds.center;
}
Vector3 Size()
{
var bounds = new Bounds(new Vector3(0, 0, 0), Vector3.zero);
bounds.Encapsulate(point1.transform.position);
bounds.Encapsulate(point2.transform.position);
return bounds.size;
}
private void Update()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = mousePos;
if (selection == true)
{
if (once == false)
{
once = true;
point1 = new GameObject().transform;
point1.position = new Vector3(mousePos.x, mousePos.y, 0f);
point2 = new GameObject().transform;
}
point2.position = new Vector3(mousePos.x, mousePos.y, 0f);
if (Vector2.Distance(point1.position, point2.position) > 1f)
{
if (oncee == false)
{
oncee = true;
CurrentBlock = Instantiate(theBlock, transform.position, Quaternion.identity);
CurrentBlock.transform.parent = point1.transform;
CurrentBlock.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
}
CurrentBlock.transform.localScale = new Vector3(Size().x, Size().y
, Size().z);
CurrentBlock.transform.position = Center();
}
}
if (Input.GetMouseButtonDown(0))
{
selection = true;
once = false;
oncee = false;
}
if (Input.GetMouseButtonUp(0))
{
Instantiate(theEffect, transform.position, Quaternion.identity);
selection = false;
once = false;
oncee = false;
Destroy(CurrentBlock, 0.1f);
Destroy(point1.gameObject);
Destroy(point2.gameObject);
}
}
}
Comment
Answer by madks13 · Jul 25, 2018 at 01:11 PM
A few errors :
You are instantiating too much. You only need to instantiate the selection GameObject once, at the begining of the drag. The rest can be Vector3
You are using transform.position as selection box's position. If transform.position == Vector3.zero so will your selection box's coordinates.
You don't need Center, use Translate/MoveTowards
Basically something like this :
using UnityEngine;
public class CursorScript : MonoBehaviour
{
public GameObject theEffect;
public GameObject theBlock;
public Vector3 begin;
public Vector3 end;
public bool selection;
private GameObject selectionBlock;
private void Start()
{
selectionBlock = Instantiate(selectionBlock, transform.position, Quaternion.identity);
selectionBlock.SetActive(false);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
selection = true;
begin = Camera.main.ScreenToWorldPoint(Input.mousePosition);
selectionBlock.SetActive(true);
//Not sure if this is what you meant
//But cursor not being visible is bad if mouse is to be used
Cursor.visible = false;
}
if (Input.GetMouseButtonUp(0))
{
Instantiate(theEffect, transform.position, Quaternion.identity);
selection = false;
Cursor.visible = true;
//Here select whatever is inside selection box
}
if (selection == true)
{
end = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Vector3.Distance(begin, end) > 1f)
{
selectionBlock.transform.localScale = begin - end;
selectionBlock.transform.position = Vector3.MoveTowards(begin, end, 0.5f);
}
}
}
}