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 jayy9 · Aug 15, 2021 at 10:46 AM · linerendererphysics.raycast

A line drawn with mouse using Linerenderer is not on exact position. How to fix it?,

I'm following a YouTube video and making a simple game where I can draw the line using the mouse position when I hold the left button down. But, the line drawn on the screen is not in the right position. I drew the line on point and the line appears at a certain offset. How do I fix it?

Here's a picture. alt text

The red cross marks are where the actual mouse was and the black line was drawn above it. Is it possible to fix this?

Here's my code

Line.cs:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Line : MonoBehaviour { public LineRenderer lineRenderer; public EdgeCollider2D edgeCollider; public Rigidbody2D rb;

 [HideInInspector] public List<Vector2> points = new List<Vector2>();
 [HideInInspector] public int pointsCount = 0;

 public float pointsMinDistance = 0.1f;
 public float CircleColliderRadius;


 public Vector2 GetLastPoint(){
     return (Vector2)lineRenderer.GetPosition(pointsCount - 1);
 }

 public void UsePhysics(bool usePhysic){
     rb.isKinematic = !usePhysic;
 }

 public void SetLineColor(Gradient LineColor){
     lineRenderer.colorGradient = LineColor;
 }

 public void SetLineWidth(float width){
     lineRenderer.startWidth = width;
     lineRenderer.endWidth = width;

     edgeCollider.edgeRadius = width/2f;
     CircleColliderRadius = width/2f;
 }

 public void AddPoint(Vector2 newPoint){
     if(pointsCount >= 1 && Vector2.Distance(newPoint, GetLastPoint()) < pointsMinDistance)
         return;

     points.Add(newPoint);
     pointsCount++;

     CircleCollider2D circleCollider = this.gameObject.AddComponent<CircleCollider2D>();
     circleCollider.offset = newPoint;
     circleCollider.radius = CircleColliderRadius;

     lineRenderer.positionCount = pointsCount;
     lineRenderer.SetPosition(pointsCount-1, newPoint);

     if(pointsCount > 1)
         edgeCollider.points = points.ToArray();
 }

 public void SetPointsMinDistance(float distance){
     pointsMinDistance = distance;
 }

}


LineDrawer.cs

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class LineDrawer : MonoBehaviour { public GameObject LinePrefabs; public LayerMask CantDrawOverLayer; int CantDrawOverLayerIndex;

 public float LinePointsMinDistance;
 public float LineWidth;
 public Gradient LineColor;

 public Rigidbody2D c1;
 public Rigidbody2D c2;

 Line currentLine;

 private void Start(){
     CantDrawOverLayerIndex = LayerMask.NameToLayer("CantDrawOver");
 }

 private void Update(){
     if(Input.GetMouseButtonDown(0)){
         BeginDraw();
     }

     if(currentLine != null)
         Draw();

     if(Input.GetMouseButtonUp(0)){
         c1.gravityScale = 1f;
         c2.gravityScale = 1f;
         EndDraw();
     }
 }
 
 public void BeginDraw(){
     currentLine = Instantiate(LinePrefabs, this.transform).GetComponent<Line>();

     currentLine.SetLineColor(LineColor);
     currentLine.SetLineWidth(LineWidth);
     currentLine.SetPointsMinDistance(LinePointsMinDistance);
     currentLine.UsePhysics(false);
 }

 public void Draw(){
     Vector2 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     RaycastHit2D hit = Physics2D.CircleCast(MousePos, LineWidth / 3f, Vector2.zero, 1f, CantDrawOverLayer);

     if(hit)
         EndDraw();
     else
         currentLine.AddPoint(MousePos);
 }

 public void EndDraw(){
     if(currentLine != null){
         if(currentLine.pointsCount < 2){
             Destroy(currentLine.gameObject);
         }
         else{
             currentLine.gameObject.layer = CantDrawOverLayerIndex;
             currentLine.UsePhysics(true);
             currentLine = null;
         }
     }
 }

}

Any help would be appreciated. Thanks :),

capture.png (8.0 kB)
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

122 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

Related Questions

Shoot on spheres 0 Answers

Arc line between two objects in world space 2 Answers

Line renderer; can you change the gradient's colorkeys/alphakeys location? 0 Answers

How to calculate inner vertices of a line renderer? (math question) 1 Answer

Function ScreenPointToLocalPointInRectangle returns different position values. 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