🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Help in drawing a line from object to indicate power (Line Renderer)

Started by
0 comments, last by it_master 4 years, 1 month ago

I may need some help in creating a line render that would draw a line from the gameobject (which is a stone) to some point in the world. This line would indicate power while having the ability to aim (which I would deal with after this, but I'll leave this for another thread) and that line would make use of the z axis since it would indicate that low power would be near the object while high power would be somewhere far from the game object while making the Z axis be near to the camera. The line would occupy the bottom of the gameobject (which is a stone)

The mechanics of the line would be similar to this video:

https://www.youtube.com/watch?v=viJxlpBMgB8

Here is the setup I had at the moment:

Testing area for throwing stones.


Here's the code that I have created for the program:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testthrow : MonoBehaviour {
   //For Object Declaration
   private GameObject stone;

   //For line render for launching
   private LineRenderer LineRender;
   private Transform transform1, transform2;
   private Vector3 mouseStart, mouseEnd, screenPoint,scanpos,offset;
   private float minFlickDistance = 10f;
   private bool islinestarting = false;

   //For the colors of the line renderer
   private Color c1, c2, c3;

   // Start is called before the first frame update
   void Start()
   {
       //Color declaration
       c1 = Color.green;
       c2 = Color.yellow;
       c3 = Color.red;

       //Declaration of necessary components
       stone = GameObject.Find("stone_throw");
       stone.AddComponent<LineRenderer>();
       LineRender = stone.GetComponent<LineRenderer>();

       //For Gradient Colors
       Gradient gradnt = new Gradient();
       float alpha = 1.0f;
       gradnt.SetKeys(
           new GradientColorKey[] { new GradientColorKey(c1, 0.0f), new GradientColorKey(c2, 0.5f), new GradientColorKey(c3, 1.0f) },
           new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 0.5f), new GradientAlphaKey(alpha, 1.0f) }
       );
       LineRender.colorGradient = gradnt;

       //Other settings for the line
       LineRender.startWidth = 0.05f;
       LineRender.endWidth = 0.025f;
       scanpos = this.transform.position;
   }

   // Update is called once per frame
   void Update()
   {
       if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
       {
           LineRender.positionCount = 0;
           mouseStart = this.transform.position;
           screenPoint = Camera.main.ScreenToWorldPoint(scanpos);

           offset = scanpos - Camera.main.ScreenToWorldPoint(
           new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y,screenPoint.z));

           LineRender.positionCount = 1;
           LineRender.SetPosition(0, offset);
           LineRender.SetPosition(1, offset);
           islinestarting = true;
       }

       if (Input.touchCount > 0 && islinestarting == true)
       {
           LineRender.positionCount = 1;
           Vector3 currentPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
           float distance = Vector3.Distance(mouseStart, currentPos);
           if (distance > minFlickDistance)
           {
               LineRender.SetPosition(LineRender.positionCount, GetWorldCoordinate(Input.GetTouch(0).position));
           }
       }

       if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
       {
           mouseEnd = Input.GetTouch(0).position;
           Touch touch = Input.GetTouch(0);
           float distance = Vector3.Distance(mouseStart, mouseEnd);
           Debug.Log("Distance: "+distance);

           if (distance > minFlickDistance)
           {
               LineRender.positionCount = 1;
               Vector3 hitPos = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 15f);
           }
           //Removes line
           LineRender.positionCount = 0;
       }
   }

   private Vector3 GetWorldCoordinate(Vector3 mousePosition)
   {
       Vector3 mousePos = new Vector3(mousePosition.x, mousePosition.y, 1);
       return Camera.main.ScreenToWorldPoint(mousePos);
   }
}

I managed to create some of the code, problem is that I'm not sure if that's the way to do it, since I wanted the line to form from the object outwards, below the screen, in order to indicate power while having the ability to aim. I created the code based on that idea, it's just a bit messed up atm.

Any help in this regard is appreciated.

Thanks in advance!

None

This topic is closed to new replies.

Advertisement