- Home /
Question by
hballamco · Feb 09, 2020 at 07:15 AM ·
gameobjecttimelinerenderergizmosradius
two objects with same volume of effect; One affect but other do not
Hello, so I made two objects. Both have the same radius of effect. Yet when I start playing, one of them works completely fine but the other affect too small volume of effect even when they have same volume of effect. This is the code for the one with problem:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LaserBeam : MonoBehaviour
{
public float damageOverTime = 3f;
public float radiusOfEffect = 30f;
public float laserOrbTime = 10f;
LineRenderer laser;
// Find closest gameobject with tag "Enemy"
public GameObject FindClosestEnemy()
{
GameObject[] enemies;
enemies = GameObject.FindGameObjectsWithTag("Enemy");
GameObject closest = null;
float distance = radiusOfEffect;
Vector3 position = transform.position;
foreach (GameObject enemy in enemies)
{
Vector3 diff = enemy.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = enemy;
distance = curDistance;
}
}
return closest;
}
// Start is called before the first frame update
void Start()
{
// Remove laser orb
Destroy(gameObject, laserOrbTime);
laser = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
if (FindClosestEnemy() != null)
{
laser.enabled = true;
laser.SetPosition(0, transform.position);
laser.SetPosition(1, FindClosestEnemy().transform.position);
// Damage
Target target = FindClosestEnemy().GetComponent<Target>();
if (target != null)
{
target.LaserDamage(damageOverTime);
}
}
else
{
laser.enabled = false;
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, radiusOfEffect);
}
}
Now this is the code of the fine one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlackHole : MonoBehaviour
{
public float radiusOfEffect = 30f;
public float BlackHoleforce = 0.5f;
public float forceIncrementOverTime = 1f;
public float damageOverTime = 3f;
public float blackHoleTime = 7f;
// Start is called before the first frame update
void Start()
{
// Remove black hole
Destroy(gameObject, blackHoleTime);
}
// Update is called once per frame
void Update()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, radiusOfEffect);
foreach (Collider nearbyObject in colliders)
{
Target target = nearbyObject.GetComponent<Target>();
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
// Add force
if (rb != null)
{
rb.AddExplosionForce(-BlackHoleforce, transform.position, radiusOfEffect);
BlackHoleforce += forceIncrementOverTime * Time.deltaTime;
}
// Damage
if (target != null)
{
target.BlackHoleDamage(damageOverTime);
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, radiusOfEffect);
}
}
Here is the picture of both objects with their gizmos drawn:
As you can see in the codes, I included a float which is radius of effect and they both the same yet in reality one affects all inside radius. Other affects really small radius even when it's gizmo shows otherwise. Please help.
capture.jpg
(180.3 kB)
Comment