- Home /
SphereCast doesn't work when I increase the radius.
using UnityEngine;
public class ChargeSpell : MonoBehaviour { public float originalDamage;
public float damage;
public float chargeSpeed;
public float chargeDamage;
public float chargeLimit;
public float maxDamage;
public float intellect;
public float baseDamage;
private float distanceToObstacle;
void Update()
{
maxDamage = intellect + baseDamage;
if(Input.GetButton("Fire1") && chargeDamage < chargeLimit)
{
damage += Time.deltaTime;
}
if(damage >= maxDamage)
{
damage = maxDamage;
}
if (Input.GetButtonUp("Fire1"))
{
Shoot();
damage = originalDamage;
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.SphereCast(transform.position, 1, transform.forward, out hit, 10))
{
Debug.Log(hit.transform.name);
distanceToObstacle = hit.distance;
EnemyHealth enemyHealth = hit.transform.GetComponent<EnemyHealth>();
if (enemyHealth != null)
{
enemyHealth.TakeDamage(damage);
}
}
}
}
When I increase the radius by anything more than 1 it just stop hitting things. I'm not sure why that one thing is broken.
Answer by Vega4Life · Dec 07, 2018 at 10:17 AM
In your case, my suspicion is the objects you want to collide with are already in the sphereCast when it was created because of it's size. When it's smaller, the objects you want to collide with are outside of the cast, thus can get a reaction.
SphereCasts only get a hit when they enter a collider, not while in it, or exit it.
As a fun thing to do for debugging. Add a sphere to your scene, set the scale to 10, 10, 10, and child it to your object casting the spell. This representation will give you a perfect visual of what is happening when the sphereCast is first created. All things in the sphere visual will not be hit.