- Home /
Question by
FranzoFoda · Apr 04, 2020 at 08:55 PM ·
c#collision
EdgeCollider2D doesn't move with Transform
I am drawing lines using a linerenderer between prefabs and my player. I want these lines to have colliders so I decided to add a EdgeCollider2D to the LineRenderer. The initial collider gets drawn right but as soon as I move the player the starting-point of the collider stays the same and doesn't follow the player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinShooting : MonoBehaviour {
public Transform firePoint;
public Transform player;
public GameObject coinPrefab;
public float lineWidth = 0.03f;
private List<GameObject> coins = new List<GameObject>();
private List<GameObject> lines = new List<GameObject>();
void Update() {
if (Input.GetKeyDown(KeyCode.Mouse0)) {
Shoot();
}
}
void FixedUpdate() {
int index = 0;
foreach (GameObject coin in coins) {
LineRenderer lr = lines[index].GetComponent<LineRenderer>();
lr.SetPosition(0, coin.transform.position);
lr.SetPosition(1, firePoint.position);
Vector2[] points = {
new Vector2(firePoint.position.x - player.position.x,
firePoint.position.y - player.position.y),
new Vector2(coin.transform.position.x - player.position.x,
coin.transform.position.y - player.position.y) };
EdgeCollider2D collider = lines[index].GetComponent<EdgeCollider2D>();
collider.points = points;
index++;
}
}
void Shoot() {
GameObject coin = Instantiate(coinPrefab, firePoint.position, firePoint.rotation);
CreateLineAndCollider(coin);
}
private void CreateLineAndCollider(GameObject coin) {
coins.Add(coin);
GameObject myLine = new GameObject();
myLine.transform.position = coin.transform.position;
LineRenderer lr = myLine.AddComponent<LineRenderer>();
lr.startWidth = lineWidth;
lr.endWidth = lineWidth;
lr.startColor = Color.blue;
lr.endColor = Color.blue;
lr.SetPosition(0, coin.transform.position);
lr.SetPosition(1, firePoint.position);
Vector2[] points = {
new Vector2(firePoint.position.x - player.position.x,
firePoint.position.y - player.position.y),
new Vector2(coin.transform.position.x - player.position.x,
coin.transform.position.y - player.position.y) };
EdgeCollider2D collider = myLine.AddComponent<EdgeCollider2D>();
collider.isTrigger = true;
collider.points = points;
lines.Add(myLine);
}
}
The collider gets drawn just fine.
But as soon as I jump the collider doesn't follow the player.
Comment
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnCollisionExit problem 1 Answer
2D collisions in Unity 4.3? 1 Answer
How to move an object over a distance in a direction? 2 Answers