- Home /
Assign Edge Collider Points to Trail Renderer Vertices
Hello, I am trying to make the player collide with the trail renderer. I have a drawing system working where when the player clicks down on the mouse button, a trail is instantiated, and begins emitting and following the mouse pointer until the player lifts the mouse button up. Along with the trail, an edge collider is instantiated. What I'd like to do now, is to se the edge collider points to the trail renderers vertices every frame. The problem I'm getting with the code below is this error:
"error CS0029: Cannot implicitly convert type 'UnityEngine.Vector3[]' to 'UnityEngine.Vector2[]'" How would I get this working? Thanks, and here's the code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PaintPlatformsPainter : MonoBehaviour
{
public GameObject trailPrefab;
public GameObject edgeColliderPrefab;
private GameObject newTrailPaint;
private GameObject newEdgeCollider;
private Vector3[] colliderPoints = new Vector3[0];
// Update is called once per frame
void Update()
{
Vector2 mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 objectPos = Camera.main.ScreenToWorldPoint(mousePos);
this.transform.position = objectPos;
if (Input.GetButtonDown ("Fire1"))
{
newTrailPaint = Instantiate(trailPrefab, this.transform.position, Quaternion.identity);
newTrailPaint.transform.SetParent(this.transform, true);
newEdgeCollider = Instantiate(edgeColliderPrefab, this.transform.position, Quaternion.identity);
newEdgeCollider.transform.SetParent(this.transform, true);
newTrailPaint.GetComponent<TrailRenderer>().emitting = true;
}
if (Input.GetButtonUp("Fire1"))
{
newTrailPaint.GetComponent<TrailRenderer>().emitting = false;
}
if (Input.GetButton("Fire1"))
{
newTrailPaint.GetComponent<TrailRenderer>().GetPositions(colliderPoints);
newEdgeCollider.GetComponent<EdgeCollider2D>().points = colliderPoints;
}
}
}
You left out the line number the error is pointing to, but try this: Use Vector3 in mouse and object positions (even you are doing 2D..) Vector3 mousePos = Input.mousePosition; mousePos.z = Camera.main.nearClipPlane; Vector3 objectPos = Camera.main.ScreenToWorldPoint(mousePos);
@CodesCove, I apologize for leaving the line number of the error out. The problem is this line: "newEdgeCollider.GetComponent().points = colliderPoints;" The problem is that the EdgeCollider2D's points are Vector2's and the I'm trying to set them to the TrailRenderer's vertices, and they are Vector3's. Thank you for your help.
Answer by Epicnez · Jul 16, 2020 at 08:11 PM
@CodesCove I got this working with this script:
using System.Collections;
using System.Collections.Generic;
//using UnityEditor.Experimental.GraphView;
using UnityEngine;
public class PaintPlatformsPainter : MonoBehaviour
{
public GameObject painter;
public GameObject trailPrefab;
public GameObject edgeColliderPrefab;
private GameObject newTrailPaint;
private GameObject newEdgeCollider;
public float scrollValue;
public int currentColor;
public Color[] colors;
public AudioSource switchSoundEffect;
public AudioSource switchSoundEffect2;
public AudioSource drawingSound;
public bool joystickReset;
// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Vertical") == 0)
{
joystickReset = true;
}
scrollValue = Input.GetAxisRaw("Mouse ScrollWheel");
painter.GetComponent<SpriteRenderer>().color = colors[currentColor];
trailPrefab.GetComponent<TrailRenderer>().startColor = colors[currentColor];
trailPrefab.GetComponent<TrailRenderer>().endColor = colors[currentColor];
if (Input.GetAxisRaw("Vertical") > 0 && joystickReset || scrollValue > 0)
{
joystickReset = false;
if (!switchSoundEffect.isPlaying)
{
switchSoundEffect.Play();
}
if (currentColor == colors.Length - 1)
{
currentColor = 0;
}
else if (currentColor < colors.Length - 1)
{
currentColor++;
}
}
else if (Input.GetAxisRaw("Vertical") < 0 && joystickReset || scrollValue < 0)
{
joystickReset = false;
if (!switchSoundEffect2.isPlaying)
{
switchSoundEffect2.Play();
}
if (currentColor == 0)
{
currentColor = colors.Length - 1;
}
else if (currentColor > 0)
{
currentColor--;
}
}
Vector2 mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 objectPos = Camera.main.ScreenToWorldPoint(mousePos);
this.transform.position = objectPos;
if (Input.GetButtonDown ("Fire1"))
{
newTrailPaint = Instantiate(trailPrefab, this.transform.position, Quaternion.identity);
newTrailPaint.transform.SetParent(this.transform, true);
newEdgeCollider = Instantiate(edgeColliderPrefab, this.transform.position, Quaternion.identity);
newTrailPaint.GetComponent<TrailRenderer>().emitting = true;
newEdgeCollider.transform.position = Vector3.zero;
}
if (Input.GetButton("Fire1"))
{
if (!drawingSound.isPlaying)
{
drawingSound.Play();
}
int maxSize = newTrailPaint.GetComponent<TrailRenderer>().positionCount;
Vector2[] trailRendererPoints = new Vector2[maxSize];
for (int i = 0; i < maxSize; i++)
{
trailRendererPoints[i] = newTrailPaint.GetComponent<TrailRenderer>().GetPosition(i);
}
newEdgeCollider.GetComponent<EdgeCollider2D>().points = trailRendererPoints;
}
if (Input.GetButtonUp("Fire1"))
{
if (!drawingSound.isPlaying)
{
drawingSound.Stop();
}
newTrailPaint.GetComponent<TrailRenderer>().emitting = false;
}
}
}
I got help from some Redditors in this link: https://www.reddit.com/r/Unity2D/comments/hmn02f/how_can_i_assign_an_edge_collider_2ds_points_to_a/ Thank you!
Your answer
Follow this Question
Related Questions
C# Convert Vector3[] to Vector2[] 3 Answers
Spawn objects at bottom of screen / camera 2 Answers
How to use Trailrenderer.Getpositions? 3 Answers
What happens when I populate a ray with a vector2? 1 Answer
Normalizing a vector 1 Answer