- Home /
Question by
frederikedel · Jul 25, 2020 at 10:08 AM ·
c#buglinerenderergolf
Line Renderer moves for no apperent reason
Hello, I have a mini golfing game where you can drag a line to shoot the ball. Its working fine, but when I move the ball to another place, the Line from the Linerenderer, which is on an empty gameobject which ist the child of the ball has an huge offset, which increases every time I move the ball. Heres the script:
using UnityEngine;
using UnityEngine.UI;
public class ballcontroll : MonoBehaviour
{
public float BallPower;
public Rigidbody2D rb;
public Transform ballPosition;
public Vector2 minPower, maxPower;
public LineRenderer line;
Camera cameraa;
Vector3 ballforce, startpoint, endpoint;
bool timeup = false;
public int tries;
public Text text;
private void Awake()
{
//line = GetComponent<LineRenderer>();
text.text = "Tries: " + tries;
}
private void Start()
{
cameraa = Camera.main;
}
private void Update()
{
if (timeup == false)
{
if (Input.GetMouseButtonDown(0))
{
//startpoint = cameraa.ScreenToWorldPoint(Input.mousePosition);
startpoint = ballPosition.position;
startpoint.z = 15;
}
if (Input.GetMouseButton(0))
{
Vector3 currentPoint = cameraa.ScreenToWorldPoint(Input.mousePosition);
currentPoint.z = 15;
Drawlint(startpoint, currentPoint);
}
if (Input.GetMouseButtonUp(0))
{
endpoint = cameraa.ScreenToWorldPoint(Input.mousePosition);
endpoint.z = 15;
ballforce = new Vector2(Mathf.Clamp(startpoint.x - endpoint.x, minPower.x, maxPower.x), Mathf.Clamp(startpoint.y - endpoint.y, minPower.y, maxPower.y));
rb.AddForce(ballforce * BallPower, ForceMode2D.Impulse);
endLine();
tries++;
text.text = "Tries: " + tries;
}
}
if (rb.velocity.magnitude >= 0)
{
timeup = true;
}
if (rb.velocity.magnitude <= 0.5 )
{
//rb.velocity = new Vector2(0, 0);
timeup = false;
}
}
public void Drawlint(Vector3 startpoint, Vector3 endpoint)
{
line.positionCount = 2;
Vector3[] Allpoint = new Vector3[2];
Allpoint[0] = startpoint;
Allpoint[1] = endpoint;
line.SetPositions(Allpoint);
}
public void endLine()
{
startpoint = Vector3.zero;
endpoint = Vector3.zero;
/*
line.SetPosition(0, Vector3.zero);
line.SetPosition(1, Vector3.zero);
Debug.Log("Diese linie");
line.positionCount = 0;*/
line.positionCount = 0;
}
}
Comment
Your answer
Follow this Question
Related Questions
Line Renderer Positioning Problem 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Items not destroying. 1 Answer
Float sets itself to zero 1 Answer