Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Epicnez · Jul 03, 2020 at 08:43 PM · collisionvector3vector2trailrendereredge

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;
 
         }
 
     }
 
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CodesCove · Jul 03, 2020 at 11:28 PM 0
Share

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);

avatar image Epicnez CodesCove · Jul 04, 2020 at 03:38 AM 0
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

218 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges