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 /
  • Help Room /
avatar image
0
Question by Frencich · May 28, 2020 at 03:05 PM · raycast2d gameline renderertrajectory

Trajectory (line renderer) not showing for some angles

Hi! i'm an Unity Beginner and i'm creating a small shooter 2D for mobile. The game is simple: there is a character located at the bottom of the screen, holding the touchscreen shows a trajectory that points towards the point touched by the player (the trajectory can bouce on the wall only once). When the player releases his finger, the character shoots.

I want to create a trajectory system exactly the same as this: Trajectory goal

The problem is my trajectory doesn't behave as it should, for some angles the line renderer doesn't show up: Trajectory angle fail

Also I can't understand how to make sure that the trajectory is always of the same length and therefore does't depend on the touch position (as it happens in my project): Trajectory distance fail

This is my trajectory script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Trajectory : MonoBehaviour
 {
     [SerializeField] private float _maxDistance = 10f;
 
     private int _count;
     private LineRenderer _line;    
 
 
     private void Start()
     {
         _line = GetComponent<LineRenderer>();        
     }
 
     // Update is called once per frame
     private void Update()
     {
         _count = 0;
         _line.positionCount = 1;
         _line.SetPosition(0, transform.position);      
         RayCast(transform.position, (Vector2)Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) - (new Vector2(transform.position.x, transform.position.y))); 
     }
 
 
     private bool RayCast(Vector2 position, Vector2 direction)
     {
         RaycastHit2D hit = Physics2D.Raycast(position, direction, _maxDistance, 1<<8); 
         if (hit && _count < 1) 
         {                     
             _count++;  
             var reflectAngle = Vector2.Reflect(direction, hit.normal);   
             _line.positionCount = _count + 1;
             _line.SetPosition(_count, hit.point);
             RayCast(hit.point + reflectAngle, reflectAngle);            
             return true;
         }
 
         if (hit == false)
         {            
             _line.positionCount = _count + 2;
             _line.SetPosition(_count + 1, position + direction * _maxDistance);
         }
         return false;
     }
 
 }
 


I really hope you can help me because I don't know what to try

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_ek98vnTRplGj8Q · May 28, 2020 at 04:36 PM

For the distance scaling problem make sure you normalize your direction so that it is always a constant length (so when you multiply with distance on line 44 you always get a vector of the same magnitude), something like _line.SetPosition(_count + 1, position + direction.normalized * _maxDistance);. For the collision problem I would guess that the problem is related... my guess is that a max distance of 10 is too small, I would try increasing it and see if you get better results. Because you are currently scaling the lineRenderer length based off of the touch distance, you are probably not raycasting all the way to the end of your line segment.

Comment
Add comment · Show 3 · 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 Frencich · May 28, 2020 at 05:36 PM 0
Share

Thanks! Your solution fixed the distance's problem! Unfortunately increasing the max distance don't fix the collision problem (i tried with 20, 50, 100). I noticed that by increasing the bounces of the trajectory, the first bounce is perfect while the second one has the same wrong behavior as before. Trajectory double bounce

I find it really strange, what can it be?

avatar image unity_ek98vnTRplGj8Q Frencich · May 28, 2020 at 07:53 PM 1
Share

Hmmm very curious... let me give you suggestions on some things you can try because I don't see any major problems that pop out at me...


  1. I would put Debug.Log statements in so you can see if its just not detecting the hit at all or is maybe reflecting back on itself for some reason

  2. You can declare variables as public to see them in real time in the inspector. I suggest making a variable to watch hit.normal, which should always be the same on each wall

  3. Try reorganizing the if statement on the raycast... I don't really trust that your if conditions are being evaluated the way you expect (hit is not a boolean)... If I had to guess I would say that this is the problem but I don't really know how c# handles evaluating non-boolean expressions to booleans so i'm not sure

      RaycastHit hit;
         
         if((count < 1) && (Physics.Raycast(blah blah blah))){
             blah blah blah
         } else{
             blah blah blah
         }
     
    
    
    
    
avatar image unity_ek98vnTRplGj8Q Frencich · May 28, 2020 at 07:55 PM 1
Share

$$anonymous$$ake sure that you add out hit as an argument to your Physics.Raycast() call so that hit is actually populated with the correct data... for example Physics.Raycast(position, direction, out hit, _maxDistance, 1<<8)

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

283 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

2D TDS Raycast: Drawray vs Line Renderer. 0 Answers

Making a GameObject follow the direction of a RayCast 1 Answer

2D Raycast Script 0 Answers

why this code doesnt work for jumping ?!(Raycasting) 0 Answers

Raycast line renderer not drawing correctly 0 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