Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 titovelosa · Feb 06, 2019 at 07:17 PM · raycast2d game2d-physicsshooterline renderer

Aim Line (Line Renderer) not showing in 2D shooter

I'm a Unity novice and I'm creating a small shooting game on a school project and I already managed to have a functional shooting system in the main character that even bounces off some objects using Unity Physics. I now wanted to integrate an aim line that will predict the trajectory of the bullet, including the bounces on objects (bubble-shooter style). I found a script that uses Raycast and Line Renderer and it supposedly does this and I tried to integrate it into my gun script but although it does not give any error, it simply does not show anything when I test the game. I do not know if the problem is in the settings that I put in the Line Renderer Component or it is in the Script. Could someone help me understand where my error is and point the right way?

My goal is:

alt text

My Line Renderer Component Definitions:

alt text

My Weapon Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(LineRenderer))]
 public class Weapon : MonoBehaviour
 {
     [Range(1, 5)]
     [SerializeField] private int _maxIterations = 3;
     [SerializeField] private float _maxDistance = 10f;
 
     public int _count;
     public LineRenderer _line;
 
     public Transform Firepoint;
     public GameObject BulletPrefab;
     public GameObject FirePrefab;
 
     void Start()
     {
         _line = GetComponent<LineRenderer>();
     }
 
     // Update is called once per frame
      void Update()
     {
       if (Input.GetButtonDown("Fire1"))
         {
             Shoot();
         }
 
         _count = 0;
         _line.SetVertexCount(1);
         _line.SetPosition(0, transform.position);
         _line.enabled = RayCast(new Ray(transform.position, transform.forward));
     }
 
     void Shoot()
     {
         //shooting logic
         var destroyBullet = Instantiate(BulletPrefab, Firepoint.position, Firepoint.rotation);
         Destroy(destroyBullet, 10f);
         var destroyFire = Instantiate(FirePrefab, Firepoint.position, Firepoint.rotation);
         Destroy(destroyFire, 0.3f);
     }
 
     private bool RayCast(Ray ray)
     {
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, _maxDistance) && _count <= _maxIterations - 1)
         {
             _count++;
             var reflectAngle = Vector3.Reflect(ray.direction, hit.normal);
             _line.SetVertexCount(_count + 1);
             _line.SetPosition(_count, hit.point);
             RayCast(new Ray(hit.point, reflectAngle));
             return true;
         }
         _line.SetVertexCount(_count + 2);
         _line.SetPosition(_count + 1, ray.GetPoint(_maxDistance));
         return false;
     }
     }




1.png (74.0 kB)
capturar.png (26.0 kB)
Comment
Add comment · Show 5
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 rballard · Feb 06, 2019 at 08:01 PM 0
Share

@titovelosa So I tried out your script and line options on my project and it seemed to work just fine... One way to debug is to check if when you play your line renderer is enabled or not. In your script on line 35 you could just remove the _line.enabled = and that would make the line show up even if it wasn't hitting. Then you could at least let you narrow it down to if the line isn't rendering at all or if your script isn't triggering it properly. But yeah I'm a little stumped because it is working on my test.

alt text

bounce.png (46.6 kB)
avatar image titovelosa rballard · Feb 06, 2019 at 08:43 PM 0
Share

@rballard , i will try to see that when im back home...i see that your project was 3D....$$anonymous$$e are 2D, perhaps something about that?

avatar image Ymrasu titovelosa · Feb 06, 2019 at 09:04 PM 0
Share

Line Renderer works in 2D, but you'll have to make sure that the z-pos makes it in front of everything (like a background) so that the camera will see it.

Show more comments
avatar image Crumpet · Feb 07, 2019 at 11:19 AM 0
Share

I copied his code and it works, the only thing I changed was the warnings related to linerenderer.setposition. I imagine your problem is that your raycast is hitting nothing so it isn't being displayed or it is hitting the things that you are instanciating.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Crumpet · Feb 07, 2019 at 05:24 AM

What part doesn't work? You're only displaying the line if it hits something?

this is your code working fine from what I can see, I haven't changed anything.

alt text

Comment
Add comment · Show 5 · 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 titovelosa · Feb 07, 2019 at 09:04 AM 0
Share

Yes, the code don't have errors but in my test the line is not showing. Perhaps because my project is 2d?!

avatar image xxmariofer titovelosa · Feb 07, 2019 at 09:26 AM 1
Share

thats not posible, share screenshots of the game view while on play mode and the inspector. i see the linerenderer is inside the weapon, and might be causing some conflicts because how line renderer works, can you try using that exact script in a object with his transform reseted to 0? if you tell your line renderer go from point 0,0,0 to 0,0,50 but your line renderer is in the point 10,15,10 for example, it will go from the 10,15,10 to the 10,15,60. take this into account.

avatar image titovelosa xxmariofer · Feb 07, 2019 at 10:40 AM 0
Share

Hi, @xxmariofer ,here the screenshots. I want a aim line attached to firepoint, before shooting.

game pic

unity game pic

2.png (153.8 kB)
1.png (69.5 kB)
Show more comments
avatar image
0

Answer by titovelosa · Feb 07, 2019 at 02:16 PM

Hi, all! Look like Unity has two physics engines one for 2D and one for 3D. The script relies on the 3D physics engine and won't work for 2D colliders. I edited the script to function with 2D colliders. Also the original script only shows the line renderer if it hits something so i made some suggested changes to fix this. So now the script looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(LineRenderer))]
 public class Weapon : MonoBehaviour
 {
     [Range(1, 5)]
     [SerializeField] private int _maxIterations = 3;
 
     [SerializeField] private float _maxDistance = 10f;
 
     public int _count;
     public LineRenderer _line;
 
     public Transform Firepoint;
     public GameObject BulletPrefab;
     public GameObject FirePrefab;
 
     private void Start()
     {
         _line = GetComponent<LineRenderer>();
     }
 
     // Update is called once per frame
     private void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             Shoot();
         }
 
         _count = 0;
         _line.SetVertexCount(1);
         _line.SetPosition(0, transform.position);
         _line.enabled = true;
         RayCast(transform.position, transform.up);
     }
 
     private void Shoot()
     {
         //shooting logic
         var destroyBullet = Instantiate(BulletPrefab, Firepoint.position, Firepoint.rotation);
         Destroy(destroyBullet, 10f);
         var destroyFire = Instantiate(FirePrefab, Firepoint.position, Firepoint.rotation);
         Destroy(destroyFire, 0.3f);
     }
 
     private bool RayCast(Vector2 position, Vector2 direction)
     {
         RaycastHit2D hit = Physics2D.Raycast(position, direction, _maxDistance);
         if (hit && _count <= _maxIterations - 1)
         {
             _count++;
             var reflectAngle = Vector2.Reflect(direction, hit.normal);
             _line.SetVertexCount(_count + 1);
             _line.SetPosition(_count, hit.point);
             RayCast(hit.point + reflectAngle, reflectAngle);
             return true;
         }
 
         if (hit == false)
         {
             _line.SetVertexCount(_count + 2);
             _line.SetPosition(_count + 1, position + direction * _maxDistance);
         }
         return false;
     }
 }


Now I already have the aim line but I will have to make some adjustments. The aim line does not point in the direction of the gun and it also seems to me that the trajectory of the bullet is different from the trajectory of the aim line, right?

Game Gif: https://ibb.co/MDsMRM0

Comment
Add comment · Show 18 · 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 Crumpet · Feb 07, 2019 at 05:44 PM 1
Share

I think transform.forward is still just a vector3 (0,0,1) by the look of it your x axis is forward so it'd need to be (1,0,0) which I think is transform.right. Not sure if that is what you were looking for but I will check it out in 2D later if you're still having problems I didn't even think about 2D being the issue

avatar image xxmariofer Crumpet · Feb 07, 2019 at 08:05 PM 1
Share

transform.forward is not (0,0,1) thats vector3.forward they are different

avatar image Crumpet xxmariofer · Feb 07, 2019 at 09:12 PM 0
Share

yes but it is 0,0,1 which is on the z axis depending on where the transform is looking, whereas vector3.forward is straight along the z axis doesn't matter what way the transform would be looking right?

Show more comments

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

157 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

Related Questions

Why does BoxCollider2D distance depend on distance to camera center? 0 Answers

Problems with 2D RaycastHit.Collider 2 Answers

Use 2D Effectors with Raycast Collision Method 0 Answers

Top Down Shooter Player Controller 1 Answer

how to make a 2d character three-dimensional? 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