Reputation: 23
I'm a noob trying to make a game like this https://youtu.be/qxwO1wyz50w?t=37 , but i cannot figure out how to add collision to my line renderers. Would a line renderer be the way to go about this or is there a better way?
Here is my code so far (messy I know im new to programming as well )
public GameObject crossHair;
public LineRenderer line;
public Transform startPoint;
public GameObject lazer;
void Start()
{
Cursor.visible = false;
}
void Update()
{
crossHair.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetKeyDown(KeyCode.Mouse0))
{
LineSpawner();
startPoint.position = crossHair.transform.position;
}
}
void LineSpawner()
{
Vector2 shootDir = crossHair.transform.position - startPoint.transform.position;
GameObject lazerInstance = Instantiate(lazer);
line = lazerInstance.GetComponent<LineRenderer>();
line.SetPosition(0, startPoint.transform.position);
line.SetPosition(1, crossHair.transform.position);
Physics2D.Raycast(startPoint.transform.position, shootDir);
Debug.DrawRay(startPoint.transform.position, shootDir, Color.red);
}
Upvotes: 0
Views: 1872
Reputation: 38
Unfortunately, you cannot add a collider component to line renderers but a way around this is to create another gameobject that is invisible and make it a child of the line itself. What you then want to do is use the invoke("targetFunctionName", amountOfSeconds)
method to destroy the collider gameobject after the player has hit it, this is so the game doesn't lag for those who are doing well while playing it. What you then want to do is add a physics material to the game object and set its bounciness property to max(which I think is 1). Do the same for the player's gameobject and you're done.
Upvotes: 1