• 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
Question by ddk4113 · Aug 03, 2017 at 02:41 PM · unity 5linerendererlinepoints

Stop LineRenderer if it collides with something

I'm making a angry birds style game, and i want to stop the line renderer if it hits or collides with another game objects at run time. is it possible by any way? i don't that the line renderer doesn't pass any game object like the example shown right now it passes throughout the wall i want to stop as it touches the wall like the second image shown, any help would be appreciated, TIA. Image

untitled.png (6.0 kB)
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by tsnyder321 · Aug 03, 2017 at 02:52 PM

Hello @ddk4113!

If I were to try what you are asking, I would use the line renderer in tandem with a raycast. First raycast along the path your line renderer will travel. If it collides with an object, store the hit point to use as the end of your line renderer. Then draw your line renderer.

Edit: After discussing this with @ddk4113 in the comments, here is a simple solution using the linecast instead of a raycast. I created the simple script below (it needs a good deal of work done to it to refine it to what you will need) but it gives you the basic idea of what I am suggesting. I was able to achieve the desired result as shown in the pictures below.

alt text alt text

You will see some initialization code in the Start function because I used 4 transforms under a parent object as my starting points for the line renderer.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(LineRenderer))]
 public class LineRenderWithCollisionDetection : MonoBehaviour
 {
 
     LineRenderer lineRenderer = null;
     Vector3[] startingLineRendererPoints = null;
 
     // Use this for initialization
     void Start () {
         //get the attacked line renderer
         lineRenderer = GetComponent<LineRenderer>();
 
         //set the max points in the line renderer. This will be custom as you need it
         lineRenderer.positionCount = 4;
         //setup the array to store the starting positions of the line renderer points. May need updated as you apply rotation, gravity, etc.
         startingLineRendererPoints = new Vector3[4];
 
         //This gets 4 transform points under the parent object that I use to first create the shape of the line renderer
         lineRenderer.SetPosition(0, transform.Find("S").position);
         lineRenderer.SetPosition(1, transform.Find("M1").position);
         lineRenderer.SetPosition(2, transform.Find("M2").position);
         lineRenderer.SetPosition(3, transform.Find("E").position);
 
         //store the starting points in the array
         lineRenderer.GetPositions(startingLineRendererPoints);
     }
     
     // Fixed update is used for physics
     void FixedUpdate ()
     {
         bool hitSomething = false;
 
         if(lineRenderer)
         {
             RaycastHit hitInfo;
             //create an array to hold the line renderer points
             Vector3[] newPointsInLine = null;
 
             for (int i = 0; i < startingLineRendererPoints.Length - 1; i++)
             {
                 if(Physics.Linecast(startingLineRendererPoints[i], startingLineRendererPoints[i + 1], out hitInfo))
                 {
 
                     Debug.Log("Line cast between " + i + " " + startingLineRendererPoints[i] + " and " + i + 1 + " " + startingLineRendererPoints[i + 1]);
 
                     //initialize the new array to the furthest point + 1 since the array is 0-based
                     newPointsInLine = new Vector3[(i + 1) + 1];
 
                     //transfer the points we need to the new array
                     for(int i2 = 0; i2 < newPointsInLine.Length; i2++)
                     {
                         newPointsInLine[i2] = startingLineRendererPoints[i2];
                     }
 
                     //set the current point to the raycast hit point (the end of the line renderer)
                     newPointsInLine[i + 1] = hitInfo.point;
 
                     //flag that we hit something
                     hitSomething = true;
 
                     break;
                 }
             }
 
             if(hitSomething)
             {
                 //use new points for the line renderer
                 lineRenderer.positionCount = newPointsInLine.Length;
 
                 lineRenderer.SetPositions(newPointsInLine);
             }
             else
             {
                 //use old points for the line renderer
                 lineRenderer.positionCount = startingLineRendererPoints.Length;
 
                 lineRenderer.SetPositions(startingLineRendererPoints);
             }
 
         }
     }
 }


snap-1.png (88.4 kB)
snap-2.png (70.4 kB)
Comment
ddk4113
GuruJeya14
TttEST_1
unity_D8A507CB721825F1B292

People who like this

4 Show 7 · 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
avatar image ddk4113 · Aug 04, 2017 at 08:11 AM 0
Share

@tsnyder321 how to raycast along a projectile?

avatar image tsnyder321 · Aug 07, 2017 at 01:20 PM 0
Share

@ddk4113 I am not sure I understand the question you are asking. Have you ever dealt with Raycasting before? If not, there are some great Unity resources to help you with that.

https://unity3d.com/learn/tutorials/topics/physics/raycasting

https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

avatar image ddk4113 tsnyder321 · Aug 09, 2017 at 04:53 AM 0
Share

@tsnyder321 yes, i did cast a ray in a straight direction, the question I'm asking is how to cast a ray in a parabolic path like if i throw an object at an angle of 45 degrees which will be using gravity and all the other physics, now to make a ray cast in that same path as that object which will use gravity and all the other effects.

avatar image tsnyder321 ddk4113 · Aug 11, 2017 at 02:20 AM 1
Share

@ddk4113

Ah! I see. When I first saw the pictures (not sure if I miss it or you updated the images) I thought it was straight lines and not curved. So my first thought is to use the positions of your line renderer as points to linecast between instead of raycast. If a linecast finds a contact point, make that the end of your line renderer and discard the extra points.

https://docs.unity3d.com/ScriptReference/Physics.Linecast.html

Show more comments
avatar image tsnyder321 · Aug 14, 2017 at 04:38 PM 0
Share

@ddk4113 I just wanted to make sure you saw my edited answer with the linecast code I used to achieve the result you were asking for? Was this what you wanted?

avatar image ddk4113 tsnyder321 · Aug 16, 2017 at 08:25 AM 0
Share

@tsnyder321 thank you. it worked in a separate 3D project ill implement in my 2D game and get back to you until then I'm accepting your answer.

[edited]

@tsnyder321 Hey, Thanks for this piece of code but somehow it's now forking in 2D i tried everything it is not blocking the objects in the 2D physics, if you could help me out here man.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

136 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

Related Questions

LineRenderer just showing awful results 3 Answers

Smooth out a line renderer 1 Answer

Issue drawing lines between buttons - Lines overlap the buttons 2 Answers

Multicolored line (path of the object) 0 Answers

LineRenderer Distortion 2 Answers


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