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
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.
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?
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...
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
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
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 }
$$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
Follow this Question
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