- Home /
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.
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. :)
Answer by ScroodgeM · Jul 22, 2012 at 02:56 PM
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 co$$anonymous$$g 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
Answer by ScroodgeM · Jul 22, 2012 at 08:34 AM
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; } }
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(Get$$anonymous$$ouseButton(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 ins$$anonymous$$d of place dots call $$anonymous$$akeADot 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 ;)
It is easy to convert this to uJS.
Check the Unity Scripting Reference, at the above-right of every script example, there is a drop-down box. Click Javascript, then select C# to see the same example in each language.
Here's some links I found useful in converting between C# and JS :
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/
If you are still having trouble, I will convert Scroodge$$anonymous$$s answer to uJS.
Answer by ratboy · Jul 22, 2012 at 02:37 PM
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.
Your answer
Follow this Question
Related Questions
Drawing lines between two touch positions? 4 Answers
Rotate on drag for IOS? 1 Answer
Rotate model with ARKit 0 Answers
Detecting touch during OnGUI () 1 Answer
Recommendations on best way to begin implementing Swipe for iOS game? 1 Answer