- Home /
Draw a line from object to indicate power
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 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 somwhere far from the game object while making the Z axis be near to the camera.
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:
Here's the code that I had right now (Yep, the code is confusing, thus I needed help in this regard):
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);
}
}
Any help in this regard is appreciated.
Thanks in advance!
Your answer
Follow this Question
Related Questions
Need to Include a file in the build and use it in the runtime 0 Answers
Change gameobjects position if it's already used by a different gameobject? 1 Answer
Android Sliding Controls 0 Answers
Polymorphism for decisions in a dialogue editor 1 Answer
Need help in the stone throwing mechanic with aiming for mobile in Unity3d 1 Answer