- Home /
This question was
closed Mar 31, 2019 at 04:18 PM by
syfen for the following reason:
The question is answered, right answer was accepted
Question by
syfen · Mar 31, 2019 at 04:04 PM ·
linerendererslingshot
Linerenderer not following game object
So I have done the first video of the angry birds tutorial and I have a problem with the line renderer. The meteor (I am using a ball instead) is supposed to have a band (line renderer) attached to it, but it's not following the ball. I have searched for solutions, but it seems like no one else is having the same problem, maybe because line renderer has been updated (IDK), and I have been searching for errors in the code, but I can't find any mistakes. Help would be appreciated. Here is a picture: Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileDragging : MonoBehaviour
{
public float maxStretch = 3.0f;
public LineRenderer catapultLineFront;
public LineRenderer catapultLineBack;
private Rigidbody2D Ball;
private SpringJoint2D spring;
private Transform catapult;
private Ray rayToMouse;
private Ray leftCatapultToProjectile;
private float maxStretchSqr;
private float circleRadius;
private bool clickedOn;
private Vector2 prevVelocity;
private void Awake()
{
spring = GetComponent<SpringJoint2D>();
catapult = spring.connectedBody.transform;
Ball = GetComponent<Rigidbody2D>();
}
void Start()
{
maxStretchSqr = maxStretch * maxStretch;
LineRendererSetup();
//catapult = GetComponent<Transform>();
leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero);
CircleCollider2D circle = GetComponent<Collider2D>() as CircleCollider2D;
circleRadius = circle.radius;
rayToMouse = new Ray(catapult.position, Vector3.zero);
}
void Update()
{
if (clickedOn)
{
Dragging();
}
if (spring != null)
{
if (!Ball.isKinematic && prevVelocity.sqrMagnitude > Ball.velocity.sqrMagnitude)
{
Destroy(spring);
Ball.velocity = prevVelocity;
}
if (!clickedOn)
{
prevVelocity = Ball.velocity;
}
}
else
{
catapultLineFront.enabled = false;
catapultLineBack.enabled = false;
}
}
void LineRendererSetup()
{
catapultLineFront.SetPosition(0, catapultLineFront.transform.position);
catapultLineBack.SetPosition(0, catapultLineBack.transform.position);
catapultLineFront.sortingLayerName = "Foreground";
catapultLineBack.sortingLayerName = "Foreground";
catapultLineFront.sortingOrder = 3;
catapultLineBack.sortingOrder = 1;
}
void OnMouseDown()
{
spring.enabled = false;
clickedOn = true;
}
private void OnMouseUp()
{
spring.enabled = true;
Ball.isKinematic = false;
clickedOn = false;
}
void Dragging()
{
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
if (catapultToMouse.sqrMagnitude > maxStretchSqr)
{
rayToMouse.direction = catapultToMouse;
mouseWorldPoint = rayToMouse.GetPoint(maxStretch);
}
mouseWorldPoint.z = 0f;
transform.position = mouseWorldPoint;
}
void LineRendererUpdate()
{
Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
leftCatapultToProjectile.direction = catapultToProjectile;
Vector3 holdPoint = leftCatapultToProjectile.GetPoint(catapultToProjectile.magnitude + circleRadius);
catapultLineFront.SetPosition(1, holdPoint);
catapultLineBack.SetPosition(1, holdPoint);
}
}
screenshot-115.png
(21.0 kB)
Comment
The linerenderer position has to be updated every frame if you want the line to move.
Thank you very much, sir, I will read my code more carefully next time-