- Home /
My Raycasts seem to sometimes miss
Basically, I am Making a script that spreads grass, The script creates a ghost seed, And if the raycast hits a certain Biome, it will spawn the seed, which then grows then rinse and repeat
It starts out working really well, So I can safely assume that the script I made isn't wrong, However... ][2]][2]
As You can see it then starts putting the seeds on the grass, But then it goes back to normal after sometime
Yes it probably wasn't the smartest idea to use 2d colliders and raycasts then try to have raycasts casting on points, However I am in too deep and it took a while to make the biomes
However they do only spread in the same biome so it isn't completely messed up
Here is the for the GhostSeed:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GhostSeedScript : MonoBehaviour
{
public GameObject Seed;
public string FoliageBiome;
void Start()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.zero);
if (hit && hit.collider.gameObject.tag != FoliageBiome)
{
Destroy(gameObject);
}
else if (hit && hit.collider.gameObject.tag == FoliageBiome)
{
Instantiate(Seed, transform.position, Quaternion.identity);
}
}
}
Ps. I tried firing the raycasts sideways but that didn't work because then the grass will spread out side the biome, Also the grass indeed has colliders :D
To further elaborate on the previous comment, you can use Debug.DrawLine() to debug raycasts.
This video shows how to do this. https://youtu.be/fFq5So-UB0E?t=949
Your answer
Follow this Question
Related Questions
2D click on object with a raycast not working 2 Answers
Is there a better way to find where to start my raycast? 2 Answers
2D Raycast not working 1 Answer
onGround raycasts return onGround is true while colliding with platform sides 0 Answers
alternative for out hit in 2D,RaycastHit2D out hit alternative 0 Answers