- Home /
drawing line on screen
Hi Guys. I am working on drawing a line on touch screen. i have done perfectly but facing a problem may be not a big problem but i have no idea what to do. when i click on mouse or touch screen the line start with the position where the object is situated and draw a strait line. i don't want the strait line to draw i want to draw a line from where i want. here is code and screen shots. thank you. public GameObject trailPrafab; GameObject thisTrail; Vector3 startPos; Plane objPlane;
void Start()
{
objPlane = new Plane(Camera.main.transform.forward * -1, this.transform.position);
}
// Update is called once per frame
void Update () {
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || Input.GetMouseButtonDown(0)){
thisTrail = (GameObject) Instantiate(trailPrafab, this.transform.position, Quaternion.identity);
Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float rayDistance;
if (objPlane.Raycast(mRay, out rayDistance))
startPos = mRay.GetPoint(rayDistance);
}
else if(((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) || Input.GetMouseButton(0)))
{
Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float rayDistance;
if (objPlane.Raycast(mRay, out rayDistance))
thisTrail.transform.position = mRay.GetPoint(rayDistance);
}
else if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) || Input.GetMouseButtonUp(0))
{
if (Vector3.Distance(thisTrail.transform.position, startPos) < 0.1)
Destroy(thisTrail);
}
}
Answer by Laniemme · May 23, 2019 at 06:13 PM
Joshius was correct in that you need the position to be startPos HOWEVER only after startPos is calculated.
Move this line:
thisTrail = (GameObject) Instantiate(trailPrafab, startPos.position, Quaternion.identity);
under this line:
thisTrail.transform.position = mRay.GetPoint(rayDistance);
Answer by toficofi · May 20, 2019 at 09:41 PM
It looks like you're putting the new trailPrefab in the same place as the script's parent is positioned, right here:
thisTrail = (GameObject) Instantiate(trailPrafab, this.transform.position, Quaternion.identity);
Try changing this.transform.position to startPos instead.
Your answer
Follow this Question
Related Questions
How to add touchscreen controls to this script? 0 Answers
Detect touches on ui element 1 Answer
Touch inputs Angry-bird style 0 Answers
How to show and hide an image by swiping 0 Answers