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?
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.
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
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(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 :)
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.
– fafasethis 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.
– ratboyits weird, have looked up the problem and tried everything (except reinstalling unity), guessing this doesnt occur with you @Scroodge?
– ratboyyou 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
– ScroodgeMPS Debug.Log mouseRay.origin and mouseRay.direction - this can point to NaN source
– ScroodgeM