- Home /
Is there a problem with my raycast?
Hello! I've been tearing my hair out over this problem for a few days now.. & at this point I'm void of any more solutions to try.
It's a very simple game, my little square bounces across platforms to the finish line to win. What I wanted to do is have him be able to shoot a ray that destroys the enemies (spheres) upon any impact with the ray. This ray would be controlled by holding down the mouse in any direction to shoot from the Player position to the target (mouse click pos). I'd already been successful making my enemies a ray to rotate around for my player to dodge, so I figured I could just copy the same logic and tweak it.
My quest eventually led me to create two different scripts, each now currently doing 1/ 2 of what I need. The images attached show one script in action and then the other. My playerlazerray1 script allows me to move the ray in any direction the mouse is at when it's clicked and held, however, upon touching enemies my ray does not trigger their destruction unless I move the ray very close. With my playerlazerray2 script, enemies are destroyed from any distance upon colliding but the line of sight hardly moves with my raycast (as seen in image attached). PlayerLazerRay1 script:
public class PlayerLazerRay : MonoBehaviour {
public float distanceRayCanSee;
public LineRenderer playerLineOfSight;
public Gradient redColor;
private Shake shake;
private Vector2 target;
bool PlayerShootingLazer;
void Start()
{
shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();
Physics2D.queriesStartInColliders = false;
}
void Update()
{
if (Input.GetMouseButton(0))
{
playerLineOfSight.enabled = true;
PlayerShootingLazer = true;
Invoke("EndLazer", 2.5f);
}
if (PlayerShootingLazer == true)
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, target, distanceRayCanSee + 50);
if (hitInfo.collider != null)
{
Debug.DrawLine(transform.position, target, Color.red);
playerLineOfSight.SetPosition(1, target);
playerLineOfSight.colorGradient = redColor;
if (hitInfo.collider.CompareTag("Enemy"))
{
print("YES");
Destroy(hitInfo.collider.gameObject);
hitInfo.collider.gameObject.SetActive(false);
}
}
else
{
Debug.DrawLine(transform.position, target * distanceRayCanSee, Color.red);
playerLineOfSight.SetPosition(1, target * distanceRayCanSee);
playerLineOfSight.colorGradient = redColor;
}
playerLineOfSight.SetPosition(0, transform.position);
}
}
void OnTriggerEnter2D(Collider2D other)
{
shake.CamShake();
}
void EndLazer()
{
playerLineOfSight.enabled = false;
PlayerShootingLazer = false;
}
}
PlayerLazerRay2 script:
public class PlayerLAzerRay2 : MonoBehaviour {
public float distanceRayCanSee;
public LineRenderer playerLineOfSight;
public Gradient redColor;
private Shake shake;
private Vector2 target;
bool PlayerShootingLazer;
void Start()
{
shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();
Physics2D.queriesStartInColliders = false;
}
void Update()
{
if (Input.GetMouseButton(0))
{
playerLineOfSight.enabled = true;
PlayerShootingLazer = true;
Invoke("EndLazer", 2.5f);
}
if (PlayerShootingLazer == true)
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, target, distanceRayCanSee + 50);
Debug.DrawLine(transform.position, target, Color.red);
playerLineOfSight.SetPosition(1, target);
playerLineOfSight.colorGradient = redColor;
if (hitInfo.collider.CompareTag("Enemy"))
{
print("YES");
Destroy(hitInfo.collider.gameObject);
hitInfo.collider.gameObject.SetActive(false);
}
else
{
Debug.DrawLine(transform.position, target * distanceRayCanSee, Color.red);
playerLineOfSight.SetPosition(1, target * distanceRayCanSee);
playerLineOfSight.colorGradient = redColor;
}
playerLineOfSight.SetPosition(0, transform.position);
}
}
void OnTriggerEnter2D(Collider2D other)
{
shake.CamShake();
}
void EndLazer()
{
playerLineOfSight.enabled = false;
PlayerShootingLazer = false;
}
}
If anyone has a solution to this, it would be extremely appreciated!
Answer by bhavinbhai2707 · Dec 26, 2019 at 11:21 AM
@ParyPro The reason it's not working is because you are giving your targets position in the direction parameter of Raycast2D. Which will never work. You need to calculate the direction to your target's position, which can be done by transform.position - target
. This will return your direction to target.
Now simply change your raycast2D to this.
RaycastHit2D hitInfo = Physics2D.Raycast(target, transform.TransformDirection(((Vector2)transform.position - target)), Mathf.Infinity);
This should work for you. Let me know if something else.
@ParyPro glad it worked out for you. If you don't $$anonymous$$d, then please consider accepting the answer. It will help others understand that the problem has been solved and can help someone who is facing the same issue. Thank you.
Your answer
Follow this Question
Related Questions
Player Raycast Does Not Compute 0 Answers
2d Raycast not work 1 Answer
What's wrong with my RaycastHit2D? 1 Answer