My team is trying to make a puzzle game where player draw line on screen as a wall/obstruct to change some ball’s moving direction.
here’s my solution so far:
void Update ()
{
//if(canDraw == true)
//{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
StartCoroutine("DrawingBlocks");
}
if(Input.GetKeyUp (KeyCode.Mouse0))
{
StopCoroutine("DrawingBlocks");
}
//}
}
void DrawSwitchOff()
{
canDraw = false;
}
void DrawSwitchOn()
{
canDraw = true;
}
IEnumerator DrawingBlocks()
{
mousePos1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
while(true)
{
yield return new WaitForSeconds(0.01f);//block spawning interval
mousePos2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
blockDrawPos = new Vector3 ((mousePos1.x + mousePos2.x) / 2, (mousePos1.y + mousePos2.y) / 2, 0);
blockRotation = new Vector3(mousePos2.x - mousePos1.x, mousePos2.y - mousePos1.y, 0);
// don't draw if mouse didn't move
if(mousePos1 != mousePos2)
{
GameObject clone;
if(canDraw == true)
{
//spawn a block in middle of 2 mouse position
clone = Instantiate(Block, blockDrawPos, Quaternion.LookRotation(blockRotation)) as GameObject;
//send message to ink monitor
inkUsed.SendMessage("inkUsed",1);
//change spawned block's scale to fill the distance between 2 mouse position
clone.gameObject.transform.localScale += new Vector3 (0, 0, Vector3.Distance(mousePos1, mousePos2));
}
}
mousePos1 = mousePos2;
}
}
It detects mouse position (before and after) every short amount of time and spawn a block(with a box collider) in between and change its scale to fill the gap, It works fine but the line seems too jagged sometime if the mouse is moving at a very low speed.
Also because of the jagged line the collider is also jagged so the balls is bouncing weird.
My question is: Is there a better way to achieve this to make the line looks smooth? And make the collider boxes connect smoothly so the balls wouldn’t bounce to unwanted direction. Thank you.