- Home /
C# More Accurate or Larger Raycast
Hi everyone, Is there a way to make a raycast more accurate or larger? I have a raycast that currently needs to be right on the object or it won't hit the object and it's rather annoying.
I did this once by having multiple raycasts close to each other. Think of a "dotted" crosshair.
Answer by Waz · Apr 30, 2013 at 02:28 AM
Sounds like what you actually mean is "less accurate" - you want it to "hit" even if not exactly on the object, yes?
For this, use a SphereCast instead of a Raycast.
I switched from Raycast to SphereCast and the console says I have some invalid arguments.What do I need to change to get this to work properly?
public float Distance = 100;
public Ray ray;
public RaycastHit hit;
if(Physics.SphereCast(ray, out hit, Distance))
I linked to the documentation. It shows how to call the function. You need to give it a radius, for example.
Ok I've made a functional spherecast.But it doesn't seem to be any better. In fact it seems like it's worse.
if(Physics.SphereCast(gameObject.transform.position, 10f, Vector3.forward, out hit, Distance))
um wow...
10 is big, 10 is a CRAZY big radius. 10 is possibly that object is inside the radius to start with.
10 is 10 meters, 30 feet. to hit something and be off by 30 feet is a pretty big rough estimate hit.
unless your firing at a space ship or something I suggest you bring the radius down to like .5 and try again.
also vector3.forward is forward in world space if you use
transform.forward you can forward for the object not the whole world.
I updated my script.But it still doesn't seem that inaccurate.
if(Physics.SphereCast(gameObject.transform.position, 5, transform.forward, out hit, Distance))
Your answer
Follow this Question
Related Questions
Finding Distance between Angles and Points 2 Answers
Getting the Furthest Point on a Ray 1 Answer
C# Raycast 2D GameObject follow Mouse 1 Answer
C# Check Physics.Raycast Once 0 Answers