Draw a line in game

Hi all, I have a 2D, sprite based physics game for IOS. Ive already implemented a script which allows the player to drag their finger on the screen and it spawns random ink splats, sort of an improvised version of that Line Rider game.

[1938-screen+shot+2012-07-16+at+13.36.35.png|1938]

Each of these inks plats is an individual prefab with the sprite image, and a capsule collider, however due to the nature of the object that rolls over it, being a rock with a capsule collider on it, occasionally the rock gets stuck in between the dots, as if the player moves their finger too quickly the engine can’t keep up and there will be gaps, as seen in the picture. (These can be a lot bigger though).

My question is if it is possible to find the position of the last dot spawned, and create a box collider between ‘this’ one, and the old one… well i know its possible, its a game engine, but how would i get the rotation ? i imagine using some sort of Quaternion.LookAt or RotateTowards or something similar, also how would i find the last spawned dot?

Cheers in advance. :slight_smile:

3 Answers

3

answer rev 3.0

draws a solid collider using box colliders folowing by mouse while pressing left button

draws in Y=0 plane independent of colliders.

example on the pic below

using UnityEngine;
using System.Collections;
public class UnbreakableCollider : MonoBehaviour
{
    public Transform DotPrefab;
    Vector3 lastDotPosition;
    bool lastPointExists;
    void Start()
    {
        lastPointExists = false;
    }
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            Vector3 newDotPosition = mouseRay.origin - mouseRay.direction / mouseRay.direction.y * mouseRay.origin.y;
            if (newDotPosition != lastDotPosition)
            {
                MakeADot(newDotPosition);
            }
        }
    }
    void MakeADot(Vector3 newDotPosition)
    {
        Transform dot =(Transform) Instantiate(DotPrefab, newDotPosition, Quaternion.identity); //use random identity to make dots looks more different
        if (lastPointExists)
        {
            GameObject colliderKeeper = new GameObject("collider");
            BoxCollider bc = colliderKeeper.AddComponent();
            colliderKeeper.transform.position = Vector3.Lerp(newDotPosition, lastDotPosition, 0.5f);
            colliderKeeper.transform.LookAt(newDotPosition);
            bc.size = new Vector3(0.1f, 0.1f, Vector3.Distance(newDotPosition, lastDotPosition));
        }
        lastDotPosition = newDotPosition;
        lastPointExists = true;
    }
}

I doubt this is not working so I transfer the +1 that was given to me. @Jacobmill you should +1 that answer too so that it shows on top for later consultation.

–

this is exactly what i need, thank you very much for you time and effort. you too @fafsase, but newDotPosition is coming through as NaN (not a number), is that a bug my side or the math? thanks for being patient.

–

its weird, have looked up the problem and tried everything (except reinstalling unity), guessing this doesnt occur with you @Scroodge?

–

you can got NaN if y-component of ray's direction is zero. this can be if your camera is orthogonal and looks toward x or z (then ray from camera will never intersect Y=0 plane you can adept ray-to-position calculation as you need - this can be other axis-oriented or even a collider intersect

–

PS Debug.Log mouseRay.origin and mouseRay.direction - this can point to NaN source

–
using UnityEngine;
using System.Collections;
public class UnbreakableCollider : MonoBehaviour
{
    public Transform DotPrefab;
    Vector3 lastDotPosition;
    bool lastPointExists;
    void Start()
    {
        //testing script
        lastPointExists = false;
        MakeADot(new Vector3(10, 0, 0));
        MakeADot(new Vector3(5, 10, 0));
        MakeADot(new Vector3(7, 7, 7));
        MakeADot(new Vector3(0, 0, 0));
        MakeADot(new Vector3(0, 10, 10));
    }
    void MakeADot(Vector3 newDotPosition)
    {
        Transform dot =(Transform) Instantiate(DotPrefab, newDotPosition, Quaternion.identity); //use random identity to make dots looks more different
        if (lastPointExists)
        {
            GameObject colliderKeeper = new GameObject("collider");
            BoxCollider bc = colliderKeeper.AddComponent();
            colliderKeeper.transform.position = Vector3.Lerp(newDotPosition, lastDotPosition, 0.5f);
            colliderKeeper.transform.LookAt(newDotPosition);
            bc.size = new Vector3(0.1f, 0.1f, Vector3.Distance(newDotPosition, lastDotPosition));
        }
        lastDotPosition = newDotPosition;
        lastPointExists = true;
    }
}

2074-1.png

Hi Scrooodge, thanks for your post. Ive implemented this and this is pretty much what i want, although being the amateur that i am, i need to be able to draw it with the mouse/finger touch. so obviously i need to start the makeadot function in update if(GetMouseButton(0)){, as this current method makes predefined lines in the Start function. So i need to make the lines as the player is drawing them, between the touch origin, to the touch end. Could you please help a little more - thanks a lot for your input already though I'm grateful as it is :)

–

take your script that places point (image in your question) and instead of place dots call MakeADot method. it places dot and makes collider at once

–

cheers scroodge, i am doing, but am getting the problem i stated in @fafase's answer. Its an edit of your script so feel free to feedback ;)

–

any chance you have a javascript version please

–

Hi Scroodge!! Now how do i remove these collider keepers??? i have put this code in the update function so it creates a lot of objects..

–

var lastDotPosition : Vector3;

var lastPointExists : boolean = false;

var startPos : Vector3;

var endPos : Vector3;

var result : Vector3;

function Update(){

 if (Input.GetMouseButtonDown(0)){

     startPos = Input.mousePosition;

 }

 if (Input.GetMouseButtonUp(0)){

     endPos = Input.mousePosition;

     result = endPos - startPos;

     Debug.Log(result);

 }

 MakeADot(result);

}

function MakeADot(newDotPosition : Vector3){

 Debug.Log(newDotPosition);

 if (Input.GetMouseButton(0)){

     var dot : Transform = Instantiate(dotPrefab, newDotPosition, Quaternion.identity);

     dot.gameObject.layer = LayerMask.NameToLayer("Play Zone");

     if (lastPointExists){

         var colliderKeeper = new GameObject("collider");

         colliderKeeper.layer = LayerMask.NameToLayer("Play Zone");

         var bc : BoxCollider = colliderKeeper.AddComponent(BoxCollider);

         colliderKeeper.transform.position = Vector3.Lerp(newDotPosition, lastDotPosition, 0.5);

         colliderKeeper.transform.LookAt(newDotPosition);

         bc.size = new Vector3 (0.1, 0.1, Vector3.Distance(newDotPosition, lastDotPosition));

     }
     lastDotPosition = newDotPosition;
     lastPointExists = true;

}

}

sorry for poor formatting, the code thing in the answers box is failing to work.

don't post commemts as answers, thanks yes, "true" 2D graphics are simply textures on a flat plane. use 2DToolkit cheers

–